Leaf Error Middleware is a piece of middleware for Vapor which allows you to return custom 404 and server error pages.
Note that this middleware is designed to be used for Leaf front-end websites only - it should not be used for providing JSON error responses for an API, for example.
First, add LeafErrorMiddleware as a dependency in your
dependencies: [ // ..., .package(name: "LeafErrorMiddleware", url: "https://github.com/brokenhandsio/leaf-error-middleware.git", from: "2.0.0") ], targets: [ .target(name: "App", dependencies: ["Vapor", ..., "LeafErrorMiddleware"]), // ... ]
To use the LeafErrorMiddleware, register the middleware service in
configure.swift to your
Application's middleware (make sure you
import LeafErrorMiddleware at the top):
Make sure it appears before all other middleware to catch errors.
You need to include two Leaf templates in your application:
When Leaf Error Middleware catches a 404 error, it will return the
404.leaf template. Any other error caught will return the
serverError.leaf template. The
serverError.leaf template will be passed up to three parameters in its context:
status- the status code of the error caught
statusMessage- a reason for the status code
reason- the reason for the error, if known. Otherwise this won't be passed in.
404.leaf template will get a
reason parameter in the context if one is known.
You may find interesting
Leaf Error Middleware 2.0.0
Updates for Vapor 4! See the README for usage details
2.0.0 Beta 2 -
LeafErrorMiddleware no longer takes an
environment in its initialiser.
reason to the templates if we have one
2.0.0 Beta 1 -
Updated for Vapor 4!
Makes the middleware useful by logging the actual error instead of swallowing it (#14)! 🥳
Leaf Error Middleware 1.2.0
- #10 - allows
Abort.redirect(to:)to pass through and redirect instead of catching it in the middleware
Leaf Error Middleware 1.1.0
Fixes in this release:
- #7 - catch errors when they are thrown inside a future
Leaf Error Middleware 1.0.0
Now supports Vapor 3! See the README for more details
1.0.0 RC 1.0 -
Leaf Error Middleware 1.0.0 RC 1.0
Adds initial support for Vapor 3 and Leaf 3!
Leaf Error Middleware 0.1.0
Initial release of Leaf Error Middleware! See the README for details on how to use!