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).
You need to add library to Package.swift
file:
.package(url: "https://github.com/Mikroservices/ExtendedError.git", from: "2.0.0")
.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()
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.
link |
Stars: 1 |
Last commit: 4 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics