🐞 ExtendedError
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()
app.middleware.use(errorMiddleware)
defer { app.shutdown() }
try configure(app)
try app.run()
Developing
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
Contributing
You can fork and clone repository. Do your changes and pull a request.