How to Exception Handler Ruby on Rails Gem?

ExceptionHandler is a “mistakes pages” gem for Ruby on Rails.

Currently, at model, the gem has been downloaded over 170,000 instances and is extensively appeared as the “best” dynamic mistakes pages gem for the framework. It works extraordinarily well.

The maximum critical issue to understand approximately ExceptionHandler is that it is essentially designed to offer a “translation” device for Rails mistakes, changing them into the precise HTTP mistakes that an internet browser can read.

Exception Handler Ruby on Rails Gem

You see, even as Rails can also additionally enhance exceptions from inside its center, those mistakes aren’t what you spot for your browser. You see the HTTP mistakes that the framework promises to the net server. This HTTP mmistakeis followed with the aid of using an “HTTP Message Body” that is what the browser shows on the page.

In different words, while managing Rails mistakes – in case you need to truly show “branded” mistakes pages (together along with your very own format or some other one) – you need to “hack” Rails to supply the ones particular pages while mistakes occur. The pages which might be proven have NO touching on what the consumer sees of their net browser; they may be in reality there to offer a “branded” manner for them to have interaction with it.

Whenever Rails increases an exception interior its utility – that mistakes (it may be something which includes a database hassle or something) is best legitimate for the Rails utility. What Rails does is “translate” those mistakes into certainly considered one among sorts of HTTP mistakes (which a browser can read): 4xx (patron mistakes) or 5xx (server mistakes).

Every time you operate a “net” utility, your pc is sending a request for records on port eighty of a publicly reachable IP address. If the alternative pc (server) has a “net server” utility jogging on port eighty, it’ll receive the request and reply with HTTP primarily based totally records (a good way to usually encompass HTML code).

The entire “net” is a public listing for the “Internet”, which means that in case you the IP (or equal area name) for a related pc, you ought to be capable of getting admission to it through the “HTTP” (HyperText Transfer Protocol). This HTTP protocol is the center of the way the “net” works, and why maximum humans get stressed while managing “mistakes” of their Rails primarily based totally programs.

HTTP “mistakes” aren’t in reality mistakes in any respect however faulty responses. Each “mistake” you spot remains an HTML page, displayed with a corresponding HTTP reputation code.

Since HTTP is a “stateless” protocol, it has to paintings with what is called a “request/reaction” pattern – basically which means that each unmarried time you ship a brand new request to an internet service, that request is dealt with as totally new.

This is adverse to “stateful” processes, which “preserve state” among requests; withinside the experience of local programs or similar. The factor is that what you spot with HTTP mistakes is a reaction to a faulty request. They’re in reality simply reputation codes that explain that.

To try this properly, you need to be capable of “translate” Rails-primarily based totally mistakes into the precise HTTP mistake reaction. This is finished with the aid of using the ActionDispatch::ShowExceptions middleware – which calls a middleware “hook” (“exceptions_app”) to decide which “HTML” reaction to reveal withinside the frame of the faulty message.

It makes use of the “rescue_responses” hash to decide which HTTP mistake code to suit any Rails exceptions to. This hash may be prolonged inside Rails, permitting customers to map distinctive Rails-primarily based totally “exceptions” to the precise HTTP reaction code.

Say your utility increases a mistake with ActiveRecord.

If it can’t discover a specific object withinside the database – Rails will enhance the ActiveRecord::NotFound exception class. This isn’t an HTTP-like-minded mistake; it is in reality a “Rails” exception. The key’s that it has to be “translated” into a mistake that HTTP-primarily based totally browsers can understand. This is wherein ActionDispatch::ShowExceptions comes in.

ShowExceptions basically takes the exception object – passes it thru the “rescue_responses” hash (to get the suitable HTTP reputation code) and – crucially – invokes the “exceptions_app” middleware hook to generate the “HTTP message frame” for the reaction…

wrapper =, exception)

reputation = wrapper.status_code

request.set_header “action_dispatch.exception”, wrapper.exception

request.set_header “action_dispatch.original_path”, request.path_info

request.path_info = “/#{reputation}”

reaction =

The key issue to understand right here is that @exceptions_app is what ExceptionHandler has been constructed to manage.

Every time Rails increases an exception, the HTTP protocol nevertheless stays regardless of what. In different words, you are continually going to require sending an HTTP reputation code and message frame to the inquiring for net browser… the distinction lies in what you ship.

The hassle for Rails is that @exceptions_app defaults to the “routes” – this means that that it’ll load 404.HTML or 500.HTML from the /public listing of your utility. Whilst this works, the pages are unprofessional and static.

To extrade the pages, ExceptionHandler overrides the “@exceptions_app” hook with its very own controller and perspectives. This approach that you are essentially capable of invoking “dynamic” net pages with Rails.

The manner ExceptionHandler works is to apply the utility’s “default” format (usually “utility”) for 4xx mistakes and encompass a very custom “exception” format for 5xx mistakes. The custom format is commonly advocated as 5xx mistakes denote server issues, this means that that in case you’re the usage of a format that references the database (as maximum “utility” layouts do), it may reason a countless loop to occur.

This approach that when you have a mistake – say ActiveRecord can’t load a file from the database – Rails will nevertheless invoke ActiveDispatch::ShowExceptions.

However, because of ExceptionHandler overriding the “exceptions_app” hook, as opposed to the static 404.HTML / 500.HTML pages being called, the controller and perspectives pipeline furnished with the aid of using the gem are invoked instead.