6

I'm pretty new in rails, and using rails 4.

In my application I want to return all 404 and 500 errors formatted on JSON like that

{
    "status": 404,
    "message": "not found"
}

There is an easy way to do this ? cause I just find solutions to do this with rails 3.x.

Thanks

I trying to do this solution Need to return JSON-formatted 404 error in Rails but I get error during failsafe response: uninitialized constant ErrorsController

Community
  • 1
  • 1
agonist_
  • 4,890
  • 6
  • 32
  • 55

1 Answers1

14

May be you're looking for this:

render :json => @error_object.to_json, :status => :unprocessable_entity

And probably you may catch all standard errors like this:

class ApplicationController < ActionController::Base
  rescue_from StandardError do |exception|
    # render what you want here
  end
end
Intrepidd
  • 19,772
  • 6
  • 55
  • 63
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79