ExtendedError

2.0.0

:beetle: Custom error middleware for Vapor
Mikroservices/ExtendedError

What's New

Support Vapor 4

2020-04-10T14:59:12Z

🐞 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()
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.

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

Last updated: Sat Jan 13 2024 20:44:55 GMT-1000 (Hawaii-Aleutian Standard Time)