@@ -31,28 +31,16 @@ public final class APIErrorMiddleware: Middleware, Service, ServiceType {
31
31
/// middleware futher down the responder chain and
32
32
/// convert it to a JSON response.
33
33
public func respond( to request: Request , chainingTo next: Responder ) throws -> Future < Response > {
34
-
35
- // We create a new promise that wraps a `Response` object.
36
- // No, there are not any initializers to do this.
37
- let result = request. eventLoop. newPromise ( Response . self)
38
-
34
+
39
35
// Call the next responder in the reponse chain.
40
36
// If the future returned contains an error, or if
41
37
// the next responder throws an error, catch it and
42
38
// convert it to a JSON response.
43
- // If no error is found, succed the promise with the response
44
- // returned by the responder.
45
- do {
46
- try next. respond ( to: request) . do { response in
47
- result. succeed ( result: response)
48
- } . catch { error in
49
- result. succeed ( result: self . response ( for: error, with: request) )
50
- }
51
- } catch {
52
- result. succeed ( result: self . response ( for: error, with: request) )
39
+ return Future . flatMap ( on: request) {
40
+ return try next. respond ( to: request)
41
+ } . mapIfError { error in
42
+ return self . response ( for: error, with: request)
53
43
}
54
-
55
- return result. futureResult
56
44
}
57
45
58
46
/// Creates a response with a JSON body.
0 commit comments