-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ErrorMiddleware to handle errors or return internal server error
- Loading branch information
1 parent
0ac58be
commit b2a4632
Showing
5 changed files
with
168 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,36 @@ | ||
public typealias Responder = (Request) async throws -> Response | ||
public typealias ErrorResponder = (Request, Error) async throws -> Response | ||
|
||
public protocol Middleware { | ||
func handle( | ||
request: Request, | ||
nextHandler: @escaping (Request) async -> Response | ||
) async -> Response | ||
responder: @escaping Responder | ||
) async throws -> Response | ||
} | ||
|
||
public extension Middleware { | ||
func handle( | ||
request: Request, | ||
responder: @escaping Responder | ||
) async throws -> Response { | ||
try await responder(request) | ||
} | ||
} | ||
|
||
public protocol ErrorMiddleware { | ||
func handle( | ||
request: Request, | ||
error: Error, | ||
responder: @escaping ErrorResponder | ||
) async throws -> Response | ||
} | ||
|
||
public extension ErrorMiddleware { | ||
func handle( | ||
request: Request, | ||
error: Error, | ||
responder: @escaping ErrorResponder | ||
) async throws -> Response { | ||
try await responder(request, error) | ||
} | ||
} |
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