# Exception Notification Plugin Question

**URL:** <https://www.ruby-forum.com/t/exception-notification-plugin-question/105014>\
**Category:** Rails\
**Created:** [May 24, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/exception-notification-plugin-question/105014 "2007-05-24T22:45:22Z")\
**Posts on this page:** 4\
**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:** [May 24, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/exception-notification-plugin-question/105014/1 "2007-05-24T22:45:22Z")

</div>

Hi,

I have two questions re the Exception Notifier plugin (i.e. which you  
install via “ruby script/plugin install exception\_notification”)

Q1 - In it’s current setup (where it uses /public/500.html) is it  
possible  
to remove the “Status: 500 Internal Server Error Content-Type:  
text/html”  
text which seems to mysteriously get place at the top of the page?

Q2 - What’s the best way to add dynamic text to the error page? Should  
I  
change the following section of the plug-in to point to a normal Rails  
action/rhtml view? But if I do this would this be making the design  
brittle, i.e. what happens if there is a Rails internal issue itself  
which  
could mean it’s not possible for the error action/rhtml view to work?  
…  
def render\_500  
respond\_to do |type|  
type.html { render :file =\> “#{RAILS\_ROOT}/public/500.html”,  
:status  
=\> “500 Error” }  
.  
.  
…

Tks  
Greg

---

<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:** [May 25, 2007, 2:33am UTC](https://www.ruby-forum.com/t/exception-notification-plugin-question/105014/2 "2007-05-25T02:33:44Z")

</div>

PS. In fact I think I may have been the scenario for which Exception  
Notification was targeted at. Would the the below be correct:

[1] Uncaught Exception (i.e. not expected)  
=\> Exception Notification handles these / sends email

[2] Caught Exceptions (understood issue - can offer specific user  
advice)  
=\> Use of custom application exception, global handler which can:  
(a) send email alert, (b) display dynamic text in custom  
application  
error view.rhtml. That is concept is you can throw a custom app  
exception anywhere in a model/controller and not have to worry  
about  
adding a handler/view.  
=\> QUESTION 1 - Where/how to catch such exception in the one spot and  
and handle this? Extend Exception Notification plugin?

[3] Complex Business Rules (in Model, but not per field)  
=\> Use of Rails Validation framework but via adding validations  
to :base within the error object

[4] Per Field Validations  
=\> Use of Rails Validations

---

<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:** [May 30, 2007, 1:02am UTC](https://www.ruby-forum.com/t/exception-notification-plugin-question/105014/3 "2007-05-30T01:02:48Z")

</div>

I would also be interested to know how to do what Greg has mentioned.

With my web application, it would help me to pindown the issue if I know  
the ID of the record in question. I’d like to add this to the exception  
error.

Does anyone know the best way to do this?

Xin

Greg H. wrote:

> Hi,
> 
> I have two questions re the Exception Notifier plugin (i.e. which you  
> install via “ruby script/plugin install exception\_notification”)
> 
> Q1 - In it’s current setup (where it uses /public/500.html) is it  
> possible  
> to remove the “Status: 500 Internal Server Error Content-Type:  
> text/html”  
> text which seems to mysteriously get place at the top of the page?
> 
> Q2 - What’s the best way to add dynamic text to the error page? Should  
> I  
> change the following section of the plug-in to point to a normal Rails  
> action/rhtml view? But if I do this would this be making the design  
> brittle, i.e. what happens if there is a Rails internal issue itself  
> which  
> could mean it’s not possible for the error action/rhtml view to work?  
> …  
> def render\_500  
> respond\_to do |type|  
> type.html { render :file =\> “#{RAILS\_ROOT}/public/500.html”,  
> :status  
> =\> “500 Error” }  
> .  
> .  
> …
> 
> Tks  
> Greg

---

<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:** [May 30, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/exception-notification-plugin-question/105014/4 "2007-05-30T21:14:14Z")

</div>

Xin,

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
