|
1 | 1 | import Vapor
|
2 | 2 |
|
3 | 3 | final class CommonErrorMiddleware: Middleware {
|
4 |
| - func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> { |
5 |
| - return next.respond(to: request).flatMapError { (error) in |
6 |
| - let headers: HTTPHeaders |
7 |
| - let status: HTTPResponseStatus |
8 |
| - switch error { |
9 |
| - case let abort as AbortError: |
10 |
| - headers = abort.headers |
11 |
| - status = abort.status |
12 |
| - default: |
13 |
| - headers = [:] |
14 |
| - status = .internalServerError |
15 |
| - } |
| 4 | + func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> { |
| 5 | + return next.respond(to: request).flatMapError { (error) in |
| 6 | + let headers: HTTPHeaders |
| 7 | + let status: HTTPResponseStatus |
| 8 | + switch error { |
| 9 | + case let abort as AbortError: |
| 10 | + headers = abort.headers |
| 11 | + status = abort.status |
| 12 | + default: |
| 13 | + headers = [:] |
| 14 | + status = .internalServerError |
| 15 | + } |
16 | 16 |
|
17 |
| - let errotTitles: [UInt: String] = [ |
18 |
| - 400: "Bad Request", |
19 |
| - 401: "Unauthorized", |
20 |
| - 403: "Access Denied", |
21 |
| - 404: "Resource not found", |
22 |
| - 500: "Webservice currently unavailable", |
23 |
| - 503: "Webservice currently unavailable", |
24 |
| - ] |
| 17 | + let errotTitles: [UInt: String] = [ |
| 18 | + 400: "Bad Request", |
| 19 | + 401: "Unauthorized", |
| 20 | + 403: "Access Denied", |
| 21 | + 404: "Resource not found", |
| 22 | + 500: "Webservice currently unavailable", |
| 23 | + 503: "Webservice currently unavailable", |
| 24 | + ] |
25 | 25 |
|
26 |
| - let errotReasons: [UInt: String] = [ |
27 |
| - 400: "The server cannot process the request due to something that is perceived to be a client error.", |
28 |
| - 401: "The requested resource requires an authentication.", |
29 |
| - 403: "The requested resource requires an authentication.", |
30 |
| - 404: "The requested resource could not be found but may be available again in the future.", |
31 |
| - 500: "An unexpected condition was encountered. Our service team has been dispatched to bring it back online.", |
32 |
| - 503: "We've got some trouble with our backend upstream cluster. Our service team has been dispatched to bring it back online.", |
33 |
| - ] |
| 26 | + let errotReasons: [UInt: String] = [ |
| 27 | + 400: "The server cannot process the request due to something that is perceived to be a client error.", |
| 28 | + 401: "The requested resource requires an authentication.", |
| 29 | + 403: "The requested resource requires an authentication.", |
| 30 | + 404: "The requested resource could not be found but may be available again in the future.", |
| 31 | + 500: "An unexpected condition was encountered. Our service team has been dispatched to bring it back online.", |
| 32 | + 503: "We've got some trouble with our backend upstream cluster. Our service team has been dispatched to bring it back online.", |
| 33 | + ] |
34 | 34 |
|
35 |
| - if request.headers[.accept].map({ $0.lowercased() }).contains("application/json") { |
36 |
| - return request.eventLoop.makeSucceededFuture(["error": status.code]) |
37 |
| - .encodeResponse(status: status, headers: headers, for: request) |
38 |
| - } else { |
39 |
| - return request.view.render("error", [ |
40 |
| - "title": "We've got some trouble", |
41 |
| - "error": errotTitles[status.code], |
42 |
| - "reason": errotReasons[status.code], |
43 |
| - "status": "\(status.code)", |
44 |
| - ]) |
45 |
| - .encodeResponse(status: status, headers: headers, for: request) |
46 |
| - } |
47 |
| - } |
| 35 | + if request.headers[.accept].map({ $0.lowercased() }).contains("application/json") { |
| 36 | + return request.eventLoop.makeSucceededFuture(["error": status.code]) |
| 37 | + .encodeResponse(status: status, headers: headers, for: request) |
| 38 | + } else { |
| 39 | + return request.view.render("error", [ |
| 40 | + "title": "We've got some trouble", |
| 41 | + "error": errotTitles[status.code], |
| 42 | + "reason": errotReasons[status.code], |
| 43 | + "status": "\(status.code)", |
| 44 | + ]) |
| 45 | + .encodeResponse(status: status, headers: headers, for: request) |
| 46 | + } |
48 | 47 | }
|
| 48 | + } |
49 | 49 | }
|
0 commit comments