Skip to content

Commit 2b6bd1f

Browse files
committed
Documented package in README
1 parent 296d915 commit 2b6bd1f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
11
# APIErrorMiddleware
22

3-
A description of this package.
3+
A middleware to catch errors from route handlers and other middleware, and convert them to a JSON response.
4+
5+
## Instillation
6+
7+
Add the package declaration to your project's manifest `dependencies` array:
8+
9+
```swift
10+
.package(url: "https://github.com/skelpo/APIErrorMiddleware.git", from: "0.1.0")
11+
```
12+
13+
Then add the `APIErrorMiddleware` library to the `dependencies` array of any target you want to access the module in.
14+
15+
## Usage
16+
17+
If you only want `APIErrorMiddleware` on some of your routes, you can create a new route group and register your routes with it:
18+
19+
```swift
20+
let api = router.group(APIErrorMiddleware())
21+
api.get(...)
22+
```
23+
24+
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.
25+
26+
You can register the middleware to the `MiddlewareConfig` using:
27+
28+
```swift
29+
middlewares.add(APIErrorMiddleware())
30+
```
31+
32+
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 commit comments

Comments
 (0)