Skip to content
This repository has been archived by the owner on Feb 7, 2020. It is now read-only.

Errors Handling

botanicus edited this page Sep 13, 2010 · 24 revisions

This page is up to date for Rango 0.1.

Introduction

There is a lot of ways how you can handle exceptions in MVC pattern. In Merb you have special Exceptions controller, Rails prefer to keep it in one controller, or you can require more complex setup … so Rango just let you set it up easily.

Internal Implementation

Exception Classes

All classes derivated from Rango::HttpError can be converted into Rack response (see Rango::HttpError#to_response).
- Exceptions classes in Rango
- List of HTTP Status Codes

Controllers

- rescue_http_error(exception) should returns standard rack array with [status, headers, body]
- render_http_error(exception) should returns just a string which will be returned as the response body.
- if you need to customize something, use exception.content_type = "text/html" and exception.headers[foo] = bar

Rails-like Exceptions Handling

Well, not exactly the same syntax, but definitely the same concept:

Merb-like Exceptions Handling

Automatic Template Rendering for Proper Error

This solution will automatically render errors/not_found if you raise NotFound error, errors/internal_server_error if you raise InternalServerError error etc.

Custom Exceptions Classes

HTTP Exceptions Outside of Controllers

As you know, in Rango you aren’t limited on controllers or whatever. Lets take a look how you can implement default exceptions handling from controllers for you custom application:

And here we are! Or, in similar way as above, you can use render "errors/#{exception.to_snakecase}" or whatever you want.

The original prototype

exc = Error302.new(“http://example.com”)
exc.headers[“foo”] = bar
raise exc
raise MovedPermanently, “http://example.com” # this is how the Rango::Controller#redirect method works