Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Exception Handling

How can I send the error messages that are happening in the model code back to the view. I mean. I have a

begin
       Some code
rescue
       Exception Handling
end

now error occurs and in the rescue, I would like to send a message back to the controller, so that it ll get displayed in the view. Do I have to use a variable, which has to contain a number of error messages that occurs in one request, concatenate them and send it back to controller, so that I can display it in the view?. Rails already shows some error messages like field can't be blank. I am asking about the other exceptions, which occurs in the functions that are present in the model code.

like image 667
felix Avatar asked Jun 27 '10 19:06

felix


People also ask

How do you handle exceptions in Rails?

Exception handling in Ruby on Rails is similar to exception handling in Ruby. Which means, we enclose the code that could raise an exception in a begin/end block and use rescue clauses to tell Ruby the types of exceptions we want to handle.

How do you handle exceptions in Ruby?

Ruby also provides a separate class for an exception that is known as an Exception class which contains different types of methods. The code in which an exception is raised, is enclosed between the begin/end block, so you can use a rescue clause to handle this type of exception.

What is begin and rescue in rails?

The code between “begin” and “rescue” is where a probable exception might occur. If an exception occurs, the rescue block will execute. You should try to be specific about what exception you're rescuing because it's considered a bad practice to capture all exceptions.

Does Ruby have try catch?

In Ruby we have a way to deal with these cases, we have begin, end(default try catch) and we can use try and catch, both try catch and raise rescue used for the same purpose, one will throw exception(throw or raise) with any specific name inside another(catch or rescue).


2 Answers

An example of what i do in my own code:

def create
  @letter = Letter.new(params[:id])

  begin
    @letter.do_something_that_could_throw_an_exception
    flash[:notice] = I18n.translate('letter.success_create')
  rescue => e
    logger.error "letter_controller::create => exception #{e.class.name} : #{e.message}"
    flash[:error] = "#{I18n.translate('letter.letter_create_failed')}<br/>Detailed error: #{e.message}"
    ExceptionNotifier.deliver_exception_notification(e, self, request)
    # redirect somewhere sensible?
  end
end

end

Does that help?

like image 92
nathanvda Avatar answered Nov 02 '22 02:11

nathanvda


Exceptions that happen as a part of saving/creating a model

I use the ActiveRecord callbacks after_validation, after_validation_on_create, and before_save (depending on the circumstance), to obtain any extra data and verify that everything is ready to be saved. Then, if any problems, I store the exception in errors[:base] using add_to_base. That way the view will display the error msg in the same way it displays any other validation errors.

Remember that if your before_save method returns false, the save will fail.

Exceptions for other model methods

All the usual methods are available:

  1. Raise a specific exception that the controller will catch. The exception can include an error number that the view translates to an error msg. Or the model can export an error_num to error_msg hash
  2. Return an error code as a return parameter of the method. Eg if you want to also use the Flash to give a positive msg when things work, you can return a msg_code. Then have negative msg codes for errors and positive codes for different types of success.
  3. Establish an @error (or whatever) instance variable to be checked by the caller.
like image 44
Larry K Avatar answered Nov 02 '22 00:11

Larry K