How do I raise an exception in Rails so it behaves like other Rails exceptions?

154,165

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.

Share:
154,165

Related videos on Youtube

Chirag Patel
Author by

Chirag Patel

I work on Rails, Javascript and lately PHP. Enjoy building stuff in general

Updated on March 13, 2020

Comments

  • Chirag Patel
    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
      levinalex over 14 years
      the 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
    Chirag Patel over 14 years
    This 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
    Jeff Paquette over 14 years
    The "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
    rcd over 10 years
    Excellent, concise answer.
  • Asaf
    Asaf almost 8 years
    The skitch link (seeing the exception with a stack trace) isn't working anymore
  • BenKoshy
    BenKoshy almost 8 years
    @levinalex will this be safe in production mode to show the stacktrace?
  • BenKoshy
    BenKoshy almost 8 years
    @levinalex - thank you alex. is there any way of adding a custom string to the error message ?
  • Neil Stockbridge
    Neil Stockbridge about 5 years
    Maybe not an answer to the OP but a very useful example, thank you!
  • Ekamjit Singh
    Ekamjit Singh almost 4 years
    It is not a good idea to raise or rescue 'Exception' itself, try to use StandardError instead. See this for details: stackoverflow.com/questions/10048173/…