diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-08-03 14:27:25 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-03 14:27:25 -0600 |
commit | 7f8bf2537db0ae596a2c1baabd4011a190666ca6 (patch) | |
tree | 3dfb8df29ef39ee5eed9bc19dc57135374a338bd /ext/fetch/26_fetch.js | |
parent | 0f07dc95f130b9ace00ad98f1b2a3f5c34662e4a (diff) |
refactor(ext/fetch): refactor fetch to use new write_error method (#20029)
This is a prerequisite for fast streams work -- this particular resource
used a custom `mpsc`-style stream, and this work will allow us to unify
it with the streams in `ext/http` in time.
Instead of using Option as an internal semaphore for "correctly
completed EOF", we allow code to propagate errors into the channel which
can be picked up by downstream sinks like Hyper. EOF is signalled using
a more standard sender drop.
Diffstat (limited to 'ext/fetch/26_fetch.js')
-rw-r--r-- | ext/fetch/26_fetch.js | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/ext/fetch/26_fetch.js b/ext/fetch/26_fetch.js index 5084fab34..6be63d077 100644 --- a/ext/fetch/26_fetch.js +++ b/ext/fetch/26_fetch.js @@ -201,6 +201,23 @@ async function mainFetch(req, recursive, terminator) { let requestSendError; let requestSendErrorSet = false; + + async function propagateError(err, message) { + // TODO(lucacasonato): propagate error into response body stream + try { + await core.writeTypeError(requestBodyRid, message); + } catch (err) { + if (!requestSendErrorSet) { + requestSendErrorSet = true; + requestSendError = err; + } + } + if (!requestSendErrorSet) { + requestSendErrorSet = true; + requestSendError = err; + } + } + if (requestBodyRid !== null) { if ( reqBody === null || @@ -220,9 +237,7 @@ async function mainFetch(req, recursive, terminator) { val = res.value; } catch (err) { if (terminator.aborted) break; - // TODO(lucacasonato): propagate error into response body stream - requestSendError = err; - requestSendErrorSet = true; + await propagateError(err, "failed to read"); break; } if (done) break; @@ -231,9 +246,7 @@ async function mainFetch(req, recursive, terminator) { "Item in request body ReadableStream is not a Uint8Array", ); await reader.cancel(error); - // TODO(lucacasonato): propagate error into response body stream - requestSendError = error; - requestSendErrorSet = true; + await propagateError(error, error.message); break; } try { @@ -241,9 +254,7 @@ async function mainFetch(req, recursive, terminator) { } catch (err) { if (terminator.aborted) break; await reader.cancel(err); - // TODO(lucacasonato): propagate error into response body stream - requestSendError = err; - requestSendErrorSet = true; + await propagateError(err, "failed to write"); break; } } @@ -252,8 +263,7 @@ async function mainFetch(req, recursive, terminator) { await core.shutdown(requestBodyRid); } catch (err) { if (!terminator.aborted) { - requestSendError = err; - requestSendErrorSet = true; + await propagateError(err, "failed to flush"); } } } |