Joe Morano
Joe Morano

Reputation: 1885

Rails default error page doesn't show on development

I built an error handler that, when there's an error in any controller method in production, reroutes the user to an error page and sends me, the developer, a notification email. This works, but I want the normal error screen to appear when I'm in development. I assumed the code to produce this was just raise e, but instead in development I'm now getting the default production error page (the one that says "We're sorry, but something went wrong".), instead of the detailed error message and trace that used to appear.

class ApplicationController < ActionController::Base
  rescue_from StandardError, with: :handle_error

  #error handler
  def handle_error(e)
    if Rails.env.production?
      #code to send email and redirect to error page
    else
      raise e
    end
  end
end

I also tried the following:

raise
raise StandardError.new e
raise e, e.message
raise e.message

and if I run any of those in a binding.pry console, they produce the error message I'm looking for, but the error page still just says "We're sorry, but something went wrong."

Anyone know how I can just show the default development error page?


UPDATE

This is insane...so the code to display an error normally should definitely work, but something somewhere is preventing that. If I change config.consider_all_requests_local = true on production, the errors show up on production, but then even if I copy and paste my config/environments/production.rb file into my config/environments/development.rb, the errors still don't show on development. If I enter a pry console, request.local? returns "0", signifying true, and Rails.env returns "development". I have no idea what is going on.

UPDATE 2

Apparently I'm not supposed to be rescuing exceptions on development, but even if I delete every bit of custom error handling code so my Application Controller is just empty, my errors still don't show on development. Further, I have a different app with the same exact error handling code, and for that the errors do show.

Upvotes: 3

Views: 4553

Answers (3)

user229044
user229044

Reputation: 239311

This is not "insane", this is completely expected behavior. You cannot raise from within a rescue_from handler. That would cause an infinite loop.

You also cannot rescue_from StandardError as stated specifically in the documentation:

Using rescue_from with Exception or StandardError would cause serious side-effects as it prevents Rails from handling exceptions properly. As such, it is not recommended to do so unless there is a strong reason.

Instead of conditionally handling the exception inside your rescue_fromhandler, you should conditionally bind the handler, and pick a more specific exception class to handle.

class ApplicationController < ActionController::Base
  rescue_from StandardError, with: :handle_error if Rails.env.production?

  #error handler
  def handle_error(e)
    #code to send email and redirect to error page
  end
end

Upvotes: 1

Leonardo Prado
Leonardo Prado

Reputation: 638

Search your code for consider_all_requests_local, it's this configuration that show the full error log.

It must be set as true on your development.rb config file. It's either missing from your configs, or other config is overwriting it

Upvotes: 4

NensiMakawana
NensiMakawana

Reputation: 54

create ErrorsController like below

class ErrorsController < ApplicationController
   skip_before_action :login

   def not_found
     respond_to do |format|
       format.html { render status: 404 }
       format.json { render json: { error: "Not found" }, status: 404 }
     end
   end
end

Upvotes: 0

Related Questions