Skip to content

Commit

Permalink
Documented package in README
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkleveter committed Apr 20, 2018
1 parent 296d915 commit 2b6bd1f
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# APIErrorMiddleware

A description of this package.
A middleware to catch errors from route handlers and other middleware, and convert them to a JSON response.

## Instillation

Add the package declaration to your project's manifest `dependencies` array:

```swift
.package(url: "https://github.com/skelpo/APIErrorMiddleware.git", from: "0.1.0")
```

Then add the `APIErrorMiddleware` library to the `dependencies` array of any target you want to access the module in.

## Usage

If you only want `APIErrorMiddleware` on some of your routes, you can create a new route group and register your routes with it:

```swift
let api = router.group(APIErrorMiddleware())
api.get(...)
```

However, if you are creating an API service and want all errors to be caught by the middleware, you probably want to add it to the your `MiddlewareConfig`. In `configure.swift`, import the APIErrorMiddleware module. The body of the `configure(_:_:_:)` function probably has a `MiddlewareConfig` instance in it. If not, create one and register it with the services.

You can register the middleware to the `MiddlewareConfig` using:

```swift
middlewares.add(APIErrorMiddleware())
```

Most likely, you will want to register this middleware first. This ensures that all the errors are caught and we don't have any thrown after its responder is run. There are some that you might want to run afterwards though, such as Vapor's built in `DateMiddleware`.

0 comments on commit 2b6bd1f

Please sign in to comment.