# Exception Handling Approach - This is what I'm doing

**URL:** <https://www.ruby-forum.com/t/exception-handling-approach-this-is-what-im-doing/127015>\
**Category:** Rails\
**Created:** [January 24, 2008, 7:12am UTC](https://www.ruby-forum.com/t/exception-handling-approach-this-is-what-im-doing/127015 "2008-01-24T07:12:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 24, 2008, 7:12am UTC](https://www.ruby-forum.com/t/exception-handling-approach-this-is-what-im-doing/127015/1 "2008-01-24T07:12:33Z")

</div>

Hi - Any feedback from people re how they handle exception handling in  
RoR? Here’s what I’m doing…

I’ve ended up doing the following so far, some of which are adjustments  
to  
Jamis B.'some “Exception Notification” plugin. I’m still optimizing  
this  
for my own usage as I go. Hope this helps (any ideas/feedback welcome):

Usage:  
if error\_situation\_occurs  
raise Error.new( “Message to User Here - Will be shown on error page”,  
“Additional message that will go to log file for  
developers”,  
Logger::ERROR)  
end

Features:

- Separate user & developer messages
- Do not have to catch such errors back in controller or views as the  
below-mentioned rails rescue frame work will handle ( i.e. less work,  
cleaner code)
- Just need to raise the Error exception which then  
triggers/implies/carries  
out:  
(a) appropriate email message to user  
(b) developer specific additional message to logs  
© severity of log entry raised  
(b) triggers Email/SMS alert based on severity of the error (use of  
Ruby  
Logger severity levels)

* * *

class Error \< RuntimeError  
attr :developer\_message  
attr :severity

## def initialize(user, dev=nil, sev=Logger::FATAL) super user # Use normal exception parameter for User Message @developer\_message = dev || user # Set developer focused message @severity = sev end end

## def rescue\_action\_in\_public(exception) # Override this Rails method case exception when \*exceptions\_to\_treat\_as\_404 render\_404 else if exception.instance\_of?(Error) # Added by Greg # Custom Error (caught) render\_custom\_error case (exception.severity) when Logger::FATAL # trigger an email/sms for FATALs send\_notification(exception) else # no emails/sms otherwise - future step: have this configurable in environment.rb end else # 500 Error (not caught) render\_500 send\_notification(exception) end end end

## def render\_custom\_error respond\_to do |type| type.html { render :template =\> “error/custom\_500\_error”} type.all { render :nothing =\> true, :status =\> “500 Error” } end end

def log\_error(exception) #:doc: # Override this Rails method  
ActiveSupport:: Deprecation.silence do  
if ActionView::TemplateError === exception  
logger.fatal(exception.to\_s)  
else  
if exception.instance\_of?(Error) # Added by Greg - logs the  
User  
Message, Log/Developer Message and limits stack trace to first 3 lines  
# Custom Error (caught)  
logger.error("#{exception.developer\_message}\n" +  
" Exception Class:#{exception.class}\n" +  
" User Msg: #{exception.message }\n" +  
" Backtrace:\n " + clean\_backtrace(exception)[0…2].join("\n  
“) + “\n\n”  
)  
else  
logger.fatal(”#{exception.developer\_message }\n" +  
" Exception Class:#{exception.class}\n" +  
" Backtrace:\n " + clean\_backtrace(exception)[0…2].join("\n  
") + “\n\n”  
)  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 24, 2008, 7:20am UTC](https://www.ruby-forum.com/t/exception-handling-approach-this-is-what-im-doing/127015/2 "2008-01-24T07:20:44Z")

</div>

class SomethingsController \< ApplicationController  
def create  
@something = Something.create!(params[:something])  
flash[:success] = “Something was created!”  
rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid  
flash[:errors] = “Something could not be created.”  
render :action =\> “new”  
end  
end

You did say any feedback.

This is currently what we’re doing and hopefully later on when other  
exceptions are raised we will be notified by email.
