class Handler < Protocol::HTTP::Middleware
- Inherits from
Protocol::HTTP::Middleware
A middleware which catches exceptions and performs an internal redirect.
Definitions
def initialize(app, location = "/errors/exception")
Implementation
def initialize(app, location = "/errors/exception")
super(app)
@location = location
end
def freeze
Freeze this object and its internal state.
Signature
-
returns
self This object.
Implementation
def freeze
return self if frozen?
@location.freeze
super
end
def call(request)
Convert application exceptions into internal-server-error responses.
Signature
-
parameter
requestUtopia::Request The request.
-
returns
Protocol::HTTP::Response The application response or a generated error response.
Implementation
def call(request)
begin
return @delegate.call(request)
rescue Exception => exception
Console.warn(self, "An error occurred while processing the request.", error: exception)
begin
# We do an internal redirection to the error location:
error_request = request.with(
method: "GET",
path: @location
)
error_request.exception = exception
error_response = Response.wrap(@delegate.call(error_request))
error_response.status = 500
return error_response
rescue Exception => exception
# If redirection fails, we also finish with a fatal error:
Console.error(self, "An error occurred while invoking the error handler.", error: exception)
return Response[500, {"content-type" => "text/plain"}, ["An error occurred while processing the request."]]
end
end
end