Uh oh!
There was an error while loading. Please reload this page.
fix(astro): Handle errors in middlewares better - #16693
Conversation
Fryuni
commented
Jun 23, 2025
The error description indicates that calling The controller may be closed by downstream code for multiple reasons, like a canceled request or a closed connection. I think (would have to look at the docs) there is a |
mydea
commented
Jun 24, 2025
ahh I see, that makes sense. Should we maybe simply not send errors from this to sentry, then 🤔 is there a value in doing this? So just: try{forawait(constchunkoforiginalBody){consthtml=typeofchunk==='string' ? chunk : decoder.decode(chunk,{stream: true});constmodifiedHtml=addMetaTagToHead(html);controller.enqueue(newTextEncoder().encode(modifiedHtml));}}catch(e){controller.error(e);}finally{controller.close();}would lose some visibility, but probably ok...? We could just |
Fryuni
commented
Jun 24, 2025
A bit tricky 🤔 The iterator can fail with a user error from a component, but the body of the loop can error out of the user's control. We'd be switching from reporting unactionable errors to not reporting actionable ones. If we hand roll the iteration using the interface and a while loop we could have the try/catch on each part with different error handlers. |
Here is one way to avoid that: // Assign to a new variable to avoid TS losing the narrower type checked above.constbody=originalBody;asyncfunction*bodyReporter(): AsyncGenerator<string|Buffer>{try{forawait(constchunkofbody){yieldchunk;}}catch(e){// Report stream errors coming from user code or Astro rendering.sendErrorToSentry(e);throwe;}}constreportedBody=bodyReporter();try{forawait(constchunkofreportedBody){consthtml=typeofchunk==='string' ? chunk : decoder.decode(chunk,{stream: true});constmodifiedHtml=addMetaTagToHead(html);controller.enqueue(newTextEncoder().encode(modifiedHtml));}}catch(e){controller.error(e);}finally{controller.close();} |
mydea
commented
Jun 25, 2025
nice, thank you, that makes sense to me! is there a good way to test that this actually works? 😅 |
controller.close() usageUh oh!
There was an error while loading. Please reload this page.
Fryuni
commented
Jun 27, 2025
Hum... A little tricky because this is deeply inside of Astro. You can make a component with |
mydea
commented
Jul 3, 2025
Hmm I will just merge this as is for now, maybe we can add a test later! |
Hopefully fixes#16491
I could not get a reproducing test there for this 🤔 I figure this is "safe" to do, but not quite sure how/when this would happen. I would assume this was "introduced" by #15995, maybe @Fryuni has a clue how/when that could happen and if this is a reasonable change 🤔