Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rescue custom exceptions coming from a middleware in Rails 3.2?

I have a Rails 3.2 application that uses Apartment, which is used as a middleware. Apartment throws an Apartment::SchemaNotFound exception and there is no way to rescue it with rescue_from from the ApplicationController. I thought I'd use config.exceptions_app as described in point #3 in this blog post, but I can't set the router as the exception app, I assume I have to create my own.

So the question is: How do I proceed?

like image 354
Robert Audi Avatar asked Jul 26 '12 10:07

Robert Audi


People also ask

How to handle exceptions in NET Core with middleware?

Custom Exception Handling in .Net Core with a Middleware 1 Exception handler Page. The simplest way is using a dedicated error page when an exception occurs. ... 2 Dedicated Status Code Pages. In many cases you want to display different error/status pages depending on the Http Status Code. ... 3 Handling the Exception with Custom Code. ...

Can I use a Rails controller as an exceptions app?

You can use any Rack application as exceptions app. Since a Rails controller is basically a Rack application, you can also use your ExceptionController directly as exceptions app. Use a lambda expression, because the controller name constant is not yet available in the initialization stage.

How do I get the status code of an exception in rails?

Since a Rails controller is basically a Rack application, you can also use your ExceptionController directly as exceptions app. Use a lambda expression, because the controller name constant is not yet available in the initialization stage. In your controller you can easily get the exception's properties (e.g. statuscode).

How do I pass an instance of exceptionhandleroptions to the middleware?

Instead, you can set the ExceptionHandler property and pass an instance of ExceptionHandlerOptions in directly to the middleware using UseExceptionHandler () if you wish: public void Configure ( IApplicationBuilder app, IWebHostEnvironment env) { app. UseExceptionHandler ( err = > err.


1 Answers

We've purposefully left Apartment pretty minimal to allow you the handle the exception itself without really needing any Rails specific setup.

I'd do something similar to what @jenn is doing above, but I wouldn't bother setting a rack env and dealing with it later, just handle the response completely in rack.

It's typical for instance that you maybe just want to redirect back to / on SchemaNotFound

You could do something like

module MyApp
  class Apartment < ::Apartment::Elevators::Subdomain
    def call(env)
      super
    rescue ::Apartment::TenantNotFound
      [302, {'Location' => '/'}, []]
    end
  end
end

This is a pretty raw handling of the exception. If you need something to happen more on the Rails side, then @jenn's answer should also work.

Check out the Rack for more details

like image 126
brad Avatar answered Sep 22 '22 03:09

brad