Swiftpack.co - Mikroservices/ExtendedError as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Mikroservices.
Mikroservices/ExtendedError 2.0.0
:beetle: Custom error middleware for Vapor
⭐️ 1
🕓 4 years ago
macOS
.package(url: "https://github.com/Mikroservices/ExtendedError.git", from: "2.0.0")

:beetle: 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.

GitHub

link
Stars: 1
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

Support Vapor 4
4 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics