AnApprentice
AnApprentice

Reputation: 110960

How to controller the error message displayed in Rails 5?

I'm working to build a Rails 5 app, not in API mode, but for an API.

One of my APIs is broken. When I load the url in the browser: http://localhost:4300/api/v1/skills.json

The browser is returning just:

500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.

Given I'm in development mode, how can I get Rails to output more helpful information like in previous versions of Rails?

Upvotes: 1

Views: 2337

Answers (1)

Antonio Ganci
Antonio Ganci

Reputation: 515

Add to application.rb the following line:

config.exceptions_app = self.routes

so you can render custom errors adding in routes:

Rails.application.routes.draw do

  get "/404" => "errors#not_found"
  get "/500" => "errors#exception"
  post "/500" => "errors#exception"
  get "/400" => "errors#exception"
  post "/400" => "errors#exception"

for example I display:

class ErrorsController < ApplicationController

  def not_found
    render :json => {:error => "not-found"}.to_json, :status => 404
  end

  def exception
    render json: {
        error: {
          message: env["action_dispatch.exception"].to_s,
          detail: env["action_dispatch.exception"]
        }
      }, 
      :status => 500
  end
end

Upvotes: 2

Related Questions