How do I raise an exception in Rails so it behaves like other Rails exceptions?
Solution 1
You don't have to do anything special, it should just be working.
When I have a fresh rails app with this controller:
class FooController < ApplicationController
def index
raise "error"
end
end
and go to http://127.0.0.1:3000/foo/
I am seeing the exception with a stack trace.
You might not see the whole stacktrace in the console log because Rails (since 2.3) filters lines from the stack trace that come from the framework itself.
See config/initializers/backtrace_silencers.rb
in your Rails project
Solution 2
You can do it like this:
class UsersController < ApplicationController
## Exception Handling
class NotActivated < StandardError
end
rescue_from NotActivated, :with => :not_activated
def not_activated(exception)
flash[:notice] = "This user is not activated."
Event.new_event "Exception: #{exception.message}", current_user, request.remote_ip
redirect_to "/"
end
def show
// Do something that fails..
raise NotActivated unless @user.is_activated?
end
end
What you're doing here is creating a class "NotActivated" that will serve as Exception. Using raise, you can throw "NotActivated" as an Exception. rescue_from is the way of catching an Exception with a specified method (not_activated in this case). Quite a long example, but it should show you how it works.
Best wishes,
Fabian
Solution 3
If you need an easier way to do it, and don't want much fuss, a simple execution could be:
raise Exception.new('something bad happened!')
This will raise an exception, say e
with e.message = something bad happened!
and then you can rescue it as you are rescuing all other exceptions in general.
Related videos on Youtube
Chirag Patel
I work on Rails, Javascript and lately PHP. Enjoy building stuff in general
Updated on March 13, 2020Comments
-
Chirag Patel about 4 years
I would like to raise an exception so that it does the same thing a normal Rails exception does. Specially, show the exception and stack trace in development mode and show "We're sorry, but something went wrong" page in production mode.
I tried the following:
raise "safety_care group missing!" if group.nil?
But it simply writes
"ERROR signing up, group missing!"
to the development.log file-
levinalex over 14 yearsthe error message you posted does not seem to come from this exception (it's a different message) is this really what you're seeing?
-
-
Chirag Patel over 14 yearsThis does not show the exception and stack trace in development mode and show "We're sorry, but something went wrong" page in production mode.
-
Jeff Paquette over 14 yearsThe "we're sorry" page is actually your web server's 500 error handler. Check your .htaccess file and you will usually see it there
-
rcd over 10 yearsExcellent, concise answer.
-
Asaf almost 8 yearsThe skitch link (seeing the exception with a stack trace) isn't working anymore
-
BenKoshy almost 8 years@levinalex will this be safe in production mode to show the stacktrace?
-
BenKoshy almost 8 years@levinalex - thank you alex. is there any way of adding a custom string to the error message ?
-
Neil Stockbridge about 5 yearsMaybe not an answer to the OP but a very useful example, thank you!
-
Ekamjit Singh almost 4 yearsIt is not a good idea to raise or rescue 'Exception' itself, try to use StandardError instead. See this for details: stackoverflow.com/questions/10048173/…