Skip to content

feat(astro): Add tracking of errors during HTML streaming #15995

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

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/astro/src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,18 @@ async function instrumentRequest(

const newResponseStream = new ReadableStream({
start: async controller => {
for await (const chunk of originalBody) {
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
const modifiedHtml = addMetaTagToHead(html);
controller.enqueue(new TextEncoder().encode(modifiedHtml));
try {
for await (const chunk of originalBody) {
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
const modifiedHtml = addMetaTagToHead(html);
controller.enqueue(new TextEncoder().encode(modifiedHtml));
}
} catch (e) {
sendErrorToSentry(e);
controller.error(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should still rethrow this error, otherwise we risk breaking people who rely on this.

Is controller.error(e); robust enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controller.error is what rethrows the error on a stream. Throwing from this function is ignored (it was being ignored before, so no one was relying on the error working).

The test is checking that the error is being forwarded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure if we want to capture this error at all. The only reason we're even going through the body is to inject our <meta> tags and if it's us causing the error by doing so, we shouldn't report it.

Or would we even swallow errors if we didn't modify the HTML?

The try/catch is probably a good idea anyway to degrade gracefully 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure if we want to capture this error at all.

This is a user thrown error that is being missed and shows as Internal Server Error on rendering. I think it makes sense for Sentry to capture and report it, but most of all it makes sense to not drop it and pass it forward so errors from the user are not discarded.

We could have some more verbose code to handle only errors from the iterator and not from any part of the <meta> tag inside the loop.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed this with @AbhiPrasad offline and for now we think it's fine to leave as-is. The injection is fairly low risk (famous last words lol). In case we get reports about our HTML injection logic causing errors, we can still revisit.

Thanks for contributing!

} finally {
controller.close();
}
controller.close();
},
});

Expand Down
43 changes: 43 additions & 0 deletions packages/astro/test/server/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,49 @@ describe('sentryMiddleware', () => {
});
});

it('throws and sends an error to sentry if response streaming throws', async () => {
const captureExceptionSpy = vi.spyOn(SentryNode, 'captureException');

const middleware = handleRequest();
const ctx = {
request: {
method: 'GET',
url: '/users',
headers: new Headers(),
},
url: new URL('https://myDomain.io/users/'),
params: {},
};

const error = new Error('Something went wrong');

const faultyStream = new ReadableStream({
pull: controller => {
controller.error(error);
controller.close();
},
});

const next = vi.fn(() =>
Promise.resolve(
new Response(faultyStream, {
headers: new Headers({ 'content-type': 'text/html' }),
}),
),
);

// @ts-expect-error, a partial ctx object is fine here
const resultFromNext = await middleware(ctx, next);

expect(resultFromNext?.headers.get('content-type')).toEqual('text/html');

await expect(() => resultFromNext!.text()).rejects.toThrowError();

expect(captureExceptionSpy).toHaveBeenCalledWith(error, {
mechanism: { handled: false, type: 'astro', data: { function: 'astroMiddleware' } },
});
});

describe('track client IP address', () => {
it('attaches client IP if `trackClientIp=true` when handling dynamic page requests', async () => {
const middleware = handleRequest({ trackClientIp: true });
Expand Down
Loading