CatchErrorMiddleware
struct CatchErrorMiddleware
Catch the specified kind of HttpError and convert it into a response.
Example
nv
use std.net.http;
use std.net.http.server.{
middlewares.CatchErrorMiddleware,
Router,
Response
};
let app = Router.new().with(CatchErrorMiddleware.new(http.NotFound, |req, e| {
return Response.text(`Fancy ${e.status()} with ${e.source()} from ${req.uri().path()}`);
}));
Implementions
Methods
new
pub fn new(status: StatusCode, f: |(Request, HttpError)|: Response): CatchErrorMiddleware
[src]
pub fn new(status: StatusCode, f: |(Request, HttpError): Response throws|): CatchErrorMiddleware {
return CatchErrorMiddleware { status, f };
}
Creates a new CatchError
middleware.
new
pub fn new(status: StatusCode, f: |(Request, HttpError)|: Response): CatchErrorMiddleware
[src]
pub fn new(status: StatusCode, f: |(Request, HttpError): Response throws|): CatchErrorMiddleware {
return CatchErrorMiddleware { status, f };
}
Creates a new CatchError
middleware.
Creates a new CatchError
middleware.
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
let resp: Response? = nil;
do {
resp = try handler.handle(request);
} catch(e: HttpError) {
if (e.status() == self.status) {
return try (self.f)(request, e);
}
throw e;
}
return resp!;
});
}
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
let resp: Response? = nil;
do {
resp = try handler.handle(request);
} catch(e: HttpError) {
if (e.status() == self.status) {
return try (self.f)(request, e);
}
throw e;
}
return resp!;
});
}