Lloople / leaf-error-middleware

Serve up custom 404 and server error pages for your Vapor App

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leaf Error Middleware

Language Build Status Code Coverage MIT License

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.

Usage

First, add LeafErrorMiddleware as a dependency in your Package.swift file:

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):

app.middleware.use(LeafErrorMiddleware())

Make sure it appears before all other middleware to catch errors.

Setting Up

You need to include two Leaf templates in your application:

  • 404.leaf
  • serverError.leaf

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.

The 404.leaf template will get a reason parameter in the context if one is known.

About

Serve up custom 404 and server error pages for your Vapor App

License:MIT License


Languages

Language:Swift 100.0%