# Rescue

**URL:** <https://www.ruby-forum.com/t/rescue/176681>\
**Category:** Rails\
**Created:** [October 4, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/rescue/176681 "2009-10-04T21:51:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 4, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/rescue/176681/1 "2009-10-04T21:51:54Z")

</div>

I am having problems with rescue actions. Currently using ‘development’  
and it is sending e-mails no problem but it still drives me to the error  
page…

My specific error at this point is  
ActionController::InvalidAuthenticityToken  
and I am using an expired session to generate the errors.

in application.rb I have…

# this part doesn’t seem to work

def rescue\_action\_in\_public(exception)  
if exception == “ActionController::InvalidAuthenticityToken”  
flash[:notice]=“Your session expired”  
redirect\_to :controller =\> ‘login’, :action =\> ‘login’  
else  
flash[:notice]=“The Application Server dumped”  
request.env[“HTTP\_REFERER”]  
redirect\_to :back  
end  
end

# this part works…I get the e-mails

protected

# Provides code to create an email generated upon error

def log\_error(exception)  
super(exception)  
begin  
ErrorMailer.deliver\_snapshot(  
exception,  
clean\_backtrace(exception),  
session.instance\_variable\_get("@data"),  
params,  
request.env)  
rescue =\> e  
logger.error(e)  
end  
end

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 4, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/rescue/176681/2 "2009-10-04T22:35:13Z")

</div>

On Oct 4, 8:50 pm, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am having problems with rescue actions. Currently using ‘development’  
> and it is sending e-mails no problem but it still drives me to the error  
> page…
> 
> My specific error at this point is  
> ActionController::InvalidAuthenticityToken  
> and I am using an expired session to generate the errors.

Your if statement is bogus, you’re just comparing an exception object  
with a string. Try using is\_a? to see if the exception object is an  
instance of a given exception class. See also rescue\_from

Fred

Fred

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 4, 2009, 10:49pm UTC](https://www.ruby-forum.com/t/rescue/176681/3 "2009-10-04T22:49:51Z")

</div>

On Sun, 2009-10-04 at 13:33 -0700, Frederick C. wrote:

> > # this part doesn’t seem to work
> > 
> > ```
> > session.instance_variable_get("@data"),
> > params,
> > request.env)
> > rescue => e
> > logger.error(e)
> > end
> > 
> > ```
> > 
> > end
> > 
> > Craig

* * *

I’ve been looking at rescue\_from (API)

I figured out that part of my problem is rescue\_action\_in\_public doesn’t  
fly for development which is fine because I can remove ‘in\_public’ while  
testing in development.

so far, if exception == “ActionController::InvalidAuthenticityToken”  
seems to be functioning as I would actually expect.

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 5, 2009, 5:54am UTC](https://www.ruby-forum.com/t/rescue/176681/4 "2009-10-05T05:54:49Z")

</div>

On Sun, 2009-10-04 at 14:07 -0700, Craig W. wrote:

> > ```
> > exception, 
> > 
> > ```
> 
> values either (well, I know if the session is deleted, I wouldn’t get
> 
> and similar for @env but both return empty sections.
> 
> but similar using @params does return values.

* * *

something changed in Rails 2.3.2 and @params and @env are good but  
@session seems to be nil - even when there is an active session.

If someone wants to toss me a bone here, I will adjust my code but for  
now, I just commented out the @session section so I can move on.

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 5, 2009, 9:41am UTC](https://www.ruby-forum.com/t/rescue/176681/5 "2009-10-05T09:41:20Z")

</div>

On Oct 5, 4:52 am, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sun, 2009-10-04 at 14:07 -0700, Craig W. wrote:
> 
> > On Sun, 2009-10-04 at 12:50 -0700, Craig W. wrote:

> * * *
> 
> something changed in Rails 2.3.2 and @params and @env are good but  
> @session seems to be nil - even when there is an active session.
> 
> If someone wants to toss me a bone here, I will adjust my code but for  
> now, I just commented out the @session section so I can move on.

Session handling was in large part rewritten for 2.3.2 - assumptions  
like there being an instance variable called @data almost certainly  
no longer hold.

Fred

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 4, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/rescue/176681/6 "2009-10-04T23:09:10Z")

</div>

On Sun, 2009-10-04 at 12:50 -0700, Craig W. wrote:

> ```
> session.instance_variable_get("@data"), 
> params, 
> request.env)
> rescue => e
> logger.error(e)
> end
> 
> ```
> 
> end

* * *

as long as I am at this, I don’t seem to get the environment or session  
values either (well, I know if the session is deleted, I wouldn’t get  
any values but when I have an error in a valid session, I would expect  
to get the session values but I don’t. @session and @env seem to  
generate errors.

I have this code in error\_mailer.rb

\<% for key, val in @session -%\>

**\<%= key %\>**

\<%= val.to\_yaml.to\_a.join("

\n

") %\>

\<% end -%\>

and similar for @env but both return empty sections.

but similar using @params does return values.

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 5, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/rescue/176681/7 "2009-10-05T16:16:31Z")

</div>

On Mon, 2009-10-05 at 00:39 -0700, Frederick C. wrote:

> > If someone wants to toss me a bone here, I will adjust my code but for  
> > now, I just commented out the @session section so I can move on.
> 
> Session handling was in large part rewritten for 2.3.2 - assumptions  
> like there being an instance variable called @data almost certainly  
> no longer hold.

* * *

I don’t know about ‘assumptions’ but I know that all of the previous  
‘how-to’ error notification wikis primarily do this…

in a protected area of your application.rb  
def log\_error(exception)  
super(exception)  
begin  
ErrorMailer.deliver\_snapshot(  
exception,  
clean\_backtrace(exception),  
session.instance\_variable\_get("@data"),  
params,  
request.env)  
rescue =\> e  
logger.error(e)  
end  
end

so it appears that  
session.instance\_variable\_get("@data")

gets me nothing of value and I wanted the current session hash here, but  
I don’t know how to get this from the changed Rails.

Yes, this is definitely changed in Rails 2.3 and pretty much every error  
notification I have found on the Internet now is broken.

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [October 5, 2009, 4:25pm UTC](https://www.ruby-forum.com/t/rescue/176681/8 "2009-10-05T16:25:18Z")

</div>

On 5 Oct 2009, at 15:14, Craig W. wrote:

> notification I have found on the Internet now is broken.

If I were you I’d stick a breakpoint somewhere around there and fiddle  
with the session object to see what it’s got.

Fred
