Posted 30 days ago. Visible to the public. Repeats.

Unpoly: Automatically show the full better_errors page when Rails raises an error

When an AJAX request raises an exception on the server, Rails will show a minimal error page with only basic information. Because all Unpoly updates work using AJAX requests, you won't get the more detailled better_errors page with the interactive REPL.

Below is an event listener that automatically repeats the request as a full-page load if your development error shows an error page. This means you get a second request, but the error page will show with full CSS and JavaScript:

Copy
// Only do this in development. This requires a `[data-environment="development"]` attribute on your `<body>` element. // See https://makandracards.com/makandra/1433-detect-the-current-rails-environment-from-javascript-or-css if (document.body.dataset.environment == 'development') { // Returns true when your development server shows an error page. function isDevelopmentError(response) { return response.isError() && /Full backtrace|Better errors/.test(response.text) } // When the development server shows an error page, repeat the request as a full page load. up.on('up:proxy:loaded', function(event) { if (isDevelopmentError(event.response)) { event.request.navigate() } }) }

The code assumes you are using Ruby on Rails with better_errors, which is the default error view that modern Rails versions employ. If you are using another method to show development errors you need to update the isDevelopmentError() function so it returns true when your particular development server shows an error.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
25 days ago
by Besprechungs-PC
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more