-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add async support to Bun.serve onError #8233
Merged
cirospaciari
merged 10 commits into
oven-sh:main
from
LukasKastern:serve-async-on-error
Jan 19, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d661344
WIP
LukasKastern 3adee9b
Support not immediately fullfilled promises
LukasKastern 547f11f
Typo
LukasKastern 3be74a4
Add tests
LukasKastern f670247
Merge branch 'main' into serve-async-on-error
LukasKastern 3f8809f
Fix test
LukasKastern 0f8c69d
Rename test
LukasKastern adc39df
Remove file
LukasKastern 32ac80d
Merge branch 'main' into serve-async-on-error
Jarred-Sumner 023a41e
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1121,6 +1121,8 @@ fn NewFlags(comptime debug_mode: bool) type { | |
response_protected: bool = false, | ||
aborted: bool = false, | ||
has_finalized: bun.DebugOnly(bool) = bun.DebugOnlyDefault(false), | ||
|
||
is_error_promise_pending: bool = false, | ||
}; | ||
} | ||
|
||
|
@@ -1377,7 +1379,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp | |
return; | ||
} | ||
|
||
if (!resp.hasResponded() and !ctx.flags.has_marked_pending) { | ||
if (!resp.hasResponded() and !ctx.flags.has_marked_pending and !ctx.flags.is_error_promise_pending) { | ||
ctx.renderMissing(); | ||
return; | ||
} | ||
|
@@ -2951,6 +2953,9 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp | |
if (result.toError()) |err| { | ||
this.finishRunningErrorHandler(err, status); | ||
return; | ||
} else if (result.asAnyPromise()) |promise| { | ||
this.processOnErrorPromise(result, promise, value, status); | ||
return; | ||
} else if (result.as(Response)) |response| { | ||
this.render(response); | ||
return; | ||
|
@@ -2961,6 +2966,75 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp | |
this.finishRunningErrorHandler(value, status); | ||
} | ||
|
||
fn processOnErrorPromise( | ||
ctx: *RequestContext, | ||
promise_js: JSC.JSValue, | ||
promise: JSC.AnyPromise, | ||
value: JSC.JSValue, | ||
status: u16, | ||
) void { | ||
var vm = ctx.server.vm; | ||
|
||
switch (promise.status(vm.global.vm())) { | ||
.Pending => {}, | ||
.Fulfilled => { | ||
const fulfilled_value = promise.result(vm.global.vm()); | ||
|
||
// if you return a Response object or a Promise<Response> | ||
// but you upgraded the connection to a WebSocket | ||
// just ignore the Response object. It doesn't do anything. | ||
// it's better to do that than to throw an error | ||
if (ctx.didUpgradeWebSocket()) { | ||
return; | ||
} | ||
|
||
var response = fulfilled_value.as(JSC.WebCore.Response) orelse { | ||
ctx.finishRunningErrorHandler(value, status); | ||
return; | ||
}; | ||
|
||
ctx.response_jsvalue = fulfilled_value; | ||
ctx.response_jsvalue.ensureStillAlive(); | ||
ctx.flags.response_protected = false; | ||
ctx.response_ptr = response; | ||
|
||
response.body.value.toBlobIfPossible(); | ||
switch (response.body.value) { | ||
.Blob => |*blob| { | ||
if (blob.needsToReadFile()) { | ||
fulfilled_value.protect(); | ||
ctx.flags.response_protected = true; | ||
} | ||
}, | ||
.Locked => { | ||
fulfilled_value.protect(); | ||
ctx.flags.response_protected = true; | ||
}, | ||
else => {}, | ||
} | ||
ctx.render(response); | ||
return; | ||
}, | ||
.Rejected => { | ||
promise.setHandled(vm.global.vm()); | ||
ctx.finishRunningErrorHandler(promise.result(vm.global.vm()), status); | ||
return; | ||
}, | ||
} | ||
|
||
// Promise is not fulfilled yet | ||
{ | ||
ctx.flags.is_error_promise_pending = true; | ||
ctx.pending_promises_for_abort += 1; | ||
promise_js.then( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added new callbacks for the error resolve/reject but realized that using |
||
ctx.server.globalThis, | ||
ctx, | ||
RequestContext.onResolve, | ||
RequestContext.onReject, | ||
); | ||
} | ||
} | ||
|
||
pub fn runErrorHandlerWithStatusCode( | ||
this: *RequestContext, | ||
value: JSC.JSValue, | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a copy of what
RequestContext.onResponse
is doing. I'm not sure if this is the correct way of handling the promise.