Preparing for Rails 2.0: Controller-based exception handling

Since Ruby is a pure Object-Oriented Language, exceptions play a big role in the flow of control. Previously, you had the choice of rescuing exceptions at a local level or you could override the rescue_action method in your controller.

The former method gave you really fine-grained control of what to do in the case of an exception:

  begin     user.save! rescue ActiveRecord::RecordInvalid     render :action => 'new' end  

In this case, if the ActiveRecord::RecordInvalid exception is raised (the save! method will raise this if validation fails) Rails will render the ‘new’ action. It became clear, though that adding begin/rescues around the same methods is pretty time consuming and not very DRY – which is where the rescue_action became helpful:

  def rescue_action(exception)   if exception == ActionView::TemplateError     render :template => 'errors/404'   else     super   end end

This (rather contrived) example will trap any ActionView::TemplateError and render the 404.erb file in the /app/views/errors directory. You are able to drop that method into any controller (including app_controller), but again, there is a lot of work involved in setting them up, making sure each controller performs the correct action for a given exception, which is why Rails 2.0 introduces rescue_from.

rescue_from is an attribute of each controller, and allows you to define a method to run when a particular exception is called. So the above example would become:

  rescue_from ActionView::TemplateError, :with => :render_404  def render_404(exception)    render :template => 'errors/404' end  

or if you prefer to use a inline block:

  rescue_from ActionView::TemplateError do { render :template => 'errors/404' } 

In the current PR release, the rescue_from technique can only catch exceptions of an exact type, so it wouldn’t catch sub-classed exceptions – if you had a custom exception called MyTemplateError that extends ActionView::TemplateError the above code won’t work. The good news is a patch to edge rails fixes this issue, so the release version of Rails 2.0 will work as expected.

Replay

Category: programming Time: 2007-11-07 Views: 1
Tags:

Related post

  • Preparing for Rails 2.0 2007-10-31

    Anyone that has used Rails 1.2.3, 1.2.4 or 1.2.5 may have noticed a number of deprecation notices in their development logs. Whilst these deprecated methods still work as expected in 1.2.x versions, you will come-a-cropper when you try to upgrade to

  • Get Your App Ready for Rails 4 2013-01-01

    A new Rails version is about to be released and it's coming with many changes, deprecations, and new features. Lets take a look at what you need to do to get your app ready for Rails 4. The Path to Upgrade The easiest way to be ready for Rails 4 is h

  • Rails ActiveRecord: Find All Users Except Current User 2010-04-20

    I feel this should be very simple but my brain is short-circuiting on it. If I have an object representing the current user, and want to query for all users except the current user, how can I do this, taking into account that the current user can som

  • The Web Design Business Kit Chapter 13 - Prepare For Expansion 2003-07-20

    In this chapter, we'll start to discuss the logistics of expanding your business. We'll discuss what it takes to move your business forward, and the all-important systems you'll need to have in place in order to grow. You'll learn why your technical

  • Preparing for Semantic Web Services 2004-05-02

    The inventor of the World Wide Web, Tim Berners-Lee, is also the originator of the next generation of Web architecture, the Semantic Web. Currently, his World Wide Web consortium (W3C) team works to develop, extend, and standardize the Web's markup l

  • Questions about preparing for advanced developer cert (501) 2014-06-04

    I have been actively studying for 501 cert and have gone through all the posts/blogs/videos about how to prepare, including another post here: How to study/prepare for the Salesforce ADVANCED Developer Certification?. But I am still very unsure about

  • How does precedence work for the Default Domain Controller Security vs the Default Domain Security Settings 2009-11-09

    How does precedence work for the Default Domain Controller Security Settings vs the Default Domain Security Settings in a Windows 2003 domain. Does one override the other? What are the conditions for this? One is defined and the other isn't, if they

  • Ubuntu desktop through pen drive for rails development 2010-06-25

    I am window user and .net web developer. I am trying hands in ruby and "ruby on rails". Everybody suggests me to use OSx or Linux. Can i install Ubuntu on "pen drive" and use as development machine for the same. If yes please let me kn

  • How to prepare for a game development competition? 2010-08-03

    Ludum Dare 18 is coming up and I'll be participating in it. I've done a couple other short-term game competitions in the past but this is my first big one (and my first solo one). I am REALLY excited for it, but I'm wondering if there are any tricks

  • How to teach Exception Handling for New Programmers? 2010-10-26

    How do you go about teaching Exception Handling to Programmers. All other things are taught easily - Data Structures, ASP.NET, WinForms, WPF, WCF - you name it, everything can be taught easily. With Exception Handling, teaching them try-catch-finally

  • What are the best practices for exception handling in n-tier applications? 2011-01-04

    What are the best practices for exception handling in n-tier applications? The application is a fat client based on MVVM pattern with .NET framework. --------------Solutions------------- The only advice I can give is that the exception and overall er

  • Preparation for Ph.D 2011-04-22

    I am going to start Ph.D (software security research) very soon. How can I prepare for that. Please recommend any tools or softwares that I need to familiarize with before stating the Ph.D. Any advices other than softwares? --------------Solutions---

  • Preparing for Interviews 2011-05-29

    Possible Duplicate: How to prepare yourself for programming interview questions? Could someone give me pointers on how go to about preparing for a technical interview? As a CS graduate, I guess one must be thorough with the following topics: Data Str

  • What is the best book to prepare for a Java interview? 2011-10-18

    Possible Duplicate: How to prepare yourself for programming interview questions? I found older questions but I was wondering if newer, better books/guides have been published in the last two years. I am interviewing for a technology analyst position

  • Preparing for algorithm-related interview questions 2011-11-07

    So, I've just realized that some employers may ask an interview question that is relative to some complex mathematical algorithm. It seems like answering these questions is a skill in its own right. After all, I'm quite good at developing algorithms

  • would preparing for CCNA add "significantly" to my knowledge as a penetration tester? 2011-11-10

    I'm a post graduate student and self taught programmer. I concentrated on Computer Networks & Operating Systems more during my undergraduate studies. I can explain usually used protocols by sniffing the traffic on wires. Operating Systems was my obse

  • Twitter Bootstrap, Less, and Sass: Understanding Your Options for Rails 3.1 2011-11-16

    Twitter Bootstrap and Rails Twitter Bootstrap, Less, and Sass: Understanding Your Options for Rails 3.1 Too good to be true! Twitter Bootstrap meets Formtastic and Tabulous How to Customize Twitter Bootstrap's Design in a Rails app By now, we've all

  • How can a PHP programmer best prepare for a C++ course? 2011-12-08

    I have never programmed in C++, but am going to be taking a class that involves programming in C++ in January. I want to be able to excel in the class, and looking for advice, as I come from a PHP background. How can I best prepare myself for success

  • What's a good way to prepare for this course titled "Programming Language Security"? 2011-12-13

    I have a course with the following description: The purpose of this course is the study of programming language security features and languages designed to support it explicitly. Static and dynamic constructs for security in languages and systems, as

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 1.281 (s). 13 q(s)