How to catch a 500 Internal Server Error in Rails

Viewed 9162

I have done this tons of times before when fetching things from a database, etc.

For my specific case I am using a 3rd party to connect to a piece of hardware... Anyways, in the case of an error, such as an invalid id obviously, we want to raise a exception or a rescue... but unfortunately I don't know how to raise it because by the time it is hit, it's too late (I think)

Here...

# 
# getting params and saving item above...
# 

if item.save
  device = RubySpark::Device.new("FAKEUNITID800")
  device.function("req", "ITEM")
  redirect_to controller: 'items', action: 'edit_items'
end

If this was a valid ID, everything would work, and it would take you to the /edit page! But the issue is, with an invalid ID, it just does...

Completed 500 Internal Server Error in 897ms

RubySpark::Device::ApiError - Permission Denied: Invalid Device ID:

I checked out the following tutorials

But honestly, they just make me more confused. Maybe I have the wrong approach to this. I always thought that first you make the request, and then you have a fall back case, depending what status (ie. 200, 500, 404) you get... you go from there.

1 Answers
Related