Skip to content

Commit d6a595c

Browse files
committed
Conformed APIErrorMiddleware to Service and ServiceType protocols
1 parent 2b6bd1f commit d6a595c

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ However, if you are creating an API service and want all errors to be caught by
2626
You can register the middleware to the `MiddlewareConfig` using:
2727

2828
```swift
29-
middlewares.add(APIErrorMiddleware())
29+
middlewares.use(APIErrorMiddleware.self)
3030
```
3131

3232
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`.

Sources/APIErrorMiddleware/APIErrorMiddleware.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import Foundation
77
///
88
/// Errors with an identifier of `modelNotFound` get
99
/// a 404 status code.
10-
public final class APIErrorMiddleware: Middleware {
10+
public final class APIErrorMiddleware: Middleware, Service, ServiceType {
1111

1212
// We define an empty init because the one
1313
// synthesized bby Swift is marked `internal`.
1414

1515
/// Create an instance if `APIErrorMiddleware`.
1616
public init() {}
1717

18+
/// Creates a service instance. Used by a `ServiceFactory`.
19+
public static func makeService(for worker: Container) throws -> APIErrorMiddleware {
20+
return APIErrorMiddleware()
21+
}
22+
1823
/// Catch all errors thrown by the route handler or
1924
/// middleware futher down the responder chain and
2025
/// convert it to a JSON response.

0 commit comments

Comments
 (0)