-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from brokenhandsio/custom-context
Allow a custom context to be used
- Loading branch information
Showing
9 changed files
with
306 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
public struct DefaultContext: Encodable { | ||
public let status: String? | ||
public let statusMessage: String? | ||
public let reason: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Sources/LeafErrorMiddleware/LeafErrroMiddlewareDefaultGenerator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Vapor | ||
|
||
public enum LeafErorrMiddlewareDefaultGenerator { | ||
static func generate(_ status: HTTPStatus, _ error: Error, _ req: Request) -> EventLoopFuture<DefaultContext> { | ||
let reason: String? | ||
if let abortError = error as? AbortError { | ||
reason = abortError.reason | ||
} else { | ||
reason = nil | ||
} | ||
let context = DefaultContext(status: status.code.description, statusMessage: status.reasonPhrase, reason: reason) | ||
return req.eventLoop.future(context ) | ||
} | ||
|
||
public static func build() -> LeafErrorMiddleware<DefaultContext> { | ||
LeafErrorMiddleware(contextGenerator: generate) | ||
} | ||
} |
Oops, something went wrong.