-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure CORS middleware sets headers when an error is thrown
- Loading branch information
1 parent
347b8db
commit 0288162
Showing
3 changed files
with
81 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 2024 the Hummingbird authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import HTTPTypes | ||
import HummingbirdCore | ||
|
||
/// Error generated from another error that adds additional headers to the response | ||
struct EditedHTTPError: HTTPResponseError { | ||
let status: HTTPResponse.Status | ||
let headers: HTTPFields | ||
let body: ByteBuffer? | ||
|
||
init(originalError: Error, additionalHeaders: HTTPFields, context: some BaseRequestContext) { | ||
if let httpError = originalError as? HTTPResponseError { | ||
self.status = httpError.status | ||
self.headers = httpError.headers + additionalHeaders | ||
self.body = httpError.body(allocator: context.allocator) | ||
} else { | ||
self.status = .internalServerError | ||
self.headers = additionalHeaders | ||
self.body = nil | ||
} | ||
} | ||
|
||
func body(allocator: NIOCore.ByteBufferAllocator) -> NIOCore.ByteBuffer? { | ||
return self.body | ||
} | ||
} |
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