jeudi 27 août 2015

Error handling with Grape, Rails and ActiveRecord

I have created a my first API service using the Grape GEM and Rails. The API's work perfectly, but I'm struggling with Error handling and the search for valuable tutorials have been without result.

In the tutorial that I followed, the only Error handling coverage they followed was this:

rescue_from ActiveRecord::RecordNotFound do |e|
    error!(message: e.message, status: 404)
end

rescue_from ActiveRecord::RecordInvalid do |e|
    error!(message: e.message, status: 422)
end

Two Issues have led me to the conclusion that this is not sufficient:

  1. I am thrown an error that indicates the following: "compared with non class/module" for ActiveRecord::RecordInvalid
  2. When I submit an API request that passes the API rules, but fails the Model validation, The request just hangs..i.e. nothing happens. I can see the response on my localhost server log (i.e. email already exists) but in Postman & and swagger it just continues loading with no response.

Can anyone point me to a tutorial that's fully focussed on Grape exception handling? I am so confused with regards to handling Model Validations through API calls etc. and could do with some guidance.

Aucun commentaire:

Enregistrer un commentaire