Home Rails rescue standard error doesn't get sent
Reply: 0

Rails rescue standard error doesn't get sent

user923
1#
user923 Published in May 21, 2018, 12:40 pm

I have a controller method as follows

def create
  @game = Game.create_new_game(game_params)
  render 'show', status: 200
rescue StandardError => e
  render json: {
    status: 500,
    error: e.to_s
   }
end

I added a binding.pry and I can clearly see the following error in my console:

#<ActiveRecord::RecordInvalid: Validation failed: Name can't be blank, Duration can't be blank>

But it still sends the status:200 to the client side. Is there a different way the errors are supposed to be handled?

EDIT:

create_new_game method in Game

def self.create_new_game(prms)
  @player = Player.find(prms[:player].to_i)
  @game = @player.games.create!(
  name: prms[:name],
  duration: prms[:duration]
  )
  @game.save!
end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.442626 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO