JSON responses for 404 and 500 errors in rails

March 13, 2013

  • Rails
  • Ruby
  • ruby-on-rails
  • actiondispatch
  • api
  • custom-response
  • internal-server-error
  • json
  • page-not-found-error
  • request
  • response
  • rest
  • routing

Rails proves to be very much helpful in creating REST APIs. Hence, while you are creating a REST API you would never want to render HTML pages for your 404(page not found) and 500(internal server) errors. This post will help you to return JSON responses instead of the default HTML templates for the same.

Prior to rails 3 you could do this easily and write the code below inside your application controller.

class ApplicationController < ActionController::Base
  rescue_from(ActionController::RoutingError) {
    render :json => {:error_message => "The resource you were looking for does not exist"}
  }
  rescue_from(Exception) {
    render :json => {:error_message => "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."}
  }
end

But with the advent of ActionDispatch in Rails 3.0 all the routing has been moved to rack DSL. Hence, now we cannot catch the ActionController::RoutingError inside of our ApplicationController, as the rack DSL will automatically give the response to the client even before the request reaches your code.

So, in order to achieve the functionality we need, we will have to change the way the routing exceptions are handled inside ActionDispatch::ShowExceptions

Step 1: Create a initializer file show_exceptions.rb inside config/initializers directory, and use the code blow:

# A Custom Exception Handler
# This will call the ErrorsController when a 404 or 500 response is to be sent

require 'action_dispatch/middleware/show_exceptions'

module ActionDispatch
  class ShowExceptions
    private
      def render_exception_with_template(env, exception)
        body = ExceptionsController.action(rescue_responses[exception.class.name]).call(env)
        log_error(exception)
        body[1]['Content-Type']= "application/json; charset=utf-8"
        body
        rescue
          render_exception_without_template(env, exception)
      end

      alias_method_chain :render_exception, :template
  end
end

This will give a call to the appropriate exception handler action inside of ExceptionsController.

Step: 2: Create ErrorsController, and use the code below

class ErrorsController < ApplicationController
  ERRORS = [
    :internal_server_error,
    :not_found
  ].freeze

  ERRORS.each do |e|
    define_method e do
      message = e == :not_found ? "The request resource was not found" : "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."

      respond_to do |format|
        format.any {render :json => {:error_message => message}, :status => e}
      end
    end
  end
end

That’s all that is required to give a custom json response for 404 and 500 errors for your restful rails applications.


LOCATION

Mumbai, Maharashtra, India

AROUND THE WEB

Copyright © 2021