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?
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. ...
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With