:beetle: Custom error middleware for Vapor

What's New

Support Vapor 4


🐞 ExtendedError

Build Status Swift 5.2 Vapor 4 Swift Package Manager Platforms OS X | Linux

Custom error middleware for Vapor. Thanks to this extended error you can create errors with additional field: code. Example:

throw Terminate(.badRequest, code: "unknownError", reason: "Unknown error occured.")

Thanks to this to client will be send below JSON:

    "error": true,
    "code": "unknownError",
    "reason": "Unknown error occured."

This is super important if you want to show user custom message based on code key (for example in different languages).

Getting started

You need to add library to Package.swift file:

  • add package to dependencies:
.package(url: "https://github.com/Mikroservices/ExtendedError.git", from: "2.0.0")
  • and add product to your target:
.target(name: "App", dependencies: [
    .product(name: "Vapor", package: "vapor"),
    .product(name: "ExtendedError", package: "ExtendedError")

Then you can add middleware to Vapor:

import App
import Vapor
import ExtendedError

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

let app = Application(env)

// Add error middleware.
let errorMiddleware = CustomErrorMiddleware()

defer { app.shutdown() }

try configure(app)
try app.run()


Download the source code and run in command line:

$ git clone https://github.com/Mikroservices/ExtendedError.git
$ swift package update
$ swift build

Run the following command if you want to open project in Xcode:

$ open Package.swift


You can fork and clone repository. Do your changes and pull a request.


  • Swift Tools 5.2.0
View More Packages from this Author


Last updated: Tue Nov 08 2022 02:07:12 GMT-0500 (GMT-05:00)