Skip to content

Commit

Permalink
fix(remix): Skip capturing ok responses as errors. (#5405)
Browse files Browse the repository at this point in the history
[Remix supports throwing responses from `loader` and `action` functions for its internal `CatchBoundary`](https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders).

They are [catched on the caller level](https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L41-L49), but as we wrap the callees, they are registered as exceptions in the SDK. Being http responses, they end up like `{size: 0}`, which is not meaningful.

This PR skips exception capturing for responses that are not `4xx` or `5xx`.
  • Loading branch information
onurtemizkan committed Jul 11, 2022
1 parent 2c6683f commit ce67a38
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/remix/src/utils/instrumentServer.ts
Expand Up @@ -69,8 +69,41 @@ interface DataFunction {
(args: DataFunctionArgs): Promise<Response> | Response | Promise<AppData> | AppData;
}

// Taken from Remix Implementation
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L54-L62
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isResponse(value: any): value is Response {
return (
value != null &&
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
typeof value.status === 'number' &&
typeof value.statusText === 'string' &&
typeof value.headers === 'object' &&
typeof value.body !== 'undefined'
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
);
}

// Taken from Remix Implementation
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L131-L145
function extractData(response: Response): Promise<unknown> {
const contentType = response.headers.get('Content-Type');

if (contentType && /\bapplication\/json\b/.test(contentType)) {
return response.json();
}

return response.text();
}

function captureRemixServerException(err: Error, name: string): void {
captureException(err, scope => {
// Skip capturing if the thrown error is an OK Response
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
if (isResponse(err) && err.status < 400) {
return;
}

captureException(isResponse(err) ? extractData(err) : err, scope => {
scope.addEventProcessor(event => {
addExceptionMechanism(event, {
type: 'instrument',
Expand Down

0 comments on commit ce67a38

Please sign in to comment.