-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(remix): Add Remix v2 support (#8415)
Adds support for new error handling utilities of Remix v2. ([ErrorBoundary](https://remix.run/docs/en/main/route/error-boundary-v2), [handleError](https://github.com/remix-run/remix/releases/tag/remix%401.17.0)) ## `ErrorBoundary` v2 Remix's `ErrorBoundary` captures all client / server / SSR errors and shows a customizable error page. In v1, to capture client-side errors we were wrapping the whole Remix application with `@sentry/react`s `ErrorBoundary` which caused inconsistencies in error pages. (See: #5762) v2 implementation does not wrap user's application with `@sentry/react`'s ErrorBoundary, instead it exports a capturing utility to be used inside the Remix application's `ErrorBoundary` function. Can be used like: ```typescript import { captureRemixErrorBoundaryError } from '@sentry/remix'; export const ErrorBoundary: V2_ErrorBoundaryComponent = () => { const error = useRouteError(); captureRemixErrorBoundaryError(error); return <div> ... </div>; }; ``` It also requires `v2_errorBoundary` [future flag](https://remix.run/docs/en/1.18.0/pages/api-development-strategy#current-future-flags) to be enabled. ## `handleError` For server-side errors apart from 'Error Responses' (thrown responses are handled in `ErrorBoundary`), this implementation exports another utility to be used in `handleError` function. The errors we capture in `handleError` also appear on `ErrorBoundary` functions but stacktraces are not available. So, we skip those errors in `captureRemixErrorBoundaryError` function. `handleError` can be instrumented as below: ```typescript export function handleError(error: unknown, { request }: DataFunctionArgs): void { if (error instanceof Error) { Sentry.captureRemixServerException(error, 'remix.server', request); } else { // Optionally Sentry.captureException(error); } ```
- Loading branch information
1 parent
4ba98e2
commit 2c3066e
Showing
17 changed files
with
225 additions
and
35 deletions.
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { captureException, withScope } from '@sentry/core'; | ||
import { addExceptionMechanism, isNodeEnv, isString } from '@sentry/utils'; | ||
|
||
import type { ErrorResponse } from '../utils/vendor/types'; | ||
|
||
/** | ||
* Checks whether the given error is an ErrorResponse. | ||
* ErrorResponse is when users throw a response from their loader or action functions. | ||
* This is in fact a server-side error that we capture on the client. | ||
* | ||
* @param error The error to check. | ||
* @returns boolean | ||
*/ | ||
function isErrorResponse(error: unknown): error is ErrorResponse { | ||
return typeof error === 'object' && error !== null && 'status' in error && 'statusText' in error; | ||
} | ||
|
||
/** | ||
* Captures an error that is thrown inside a Remix ErrorBoundary. | ||
* | ||
* @param error The error to capture. | ||
* @returns void | ||
*/ | ||
export function captureRemixErrorBoundaryError(error: unknown): void { | ||
const isClientSideRuntimeError = !isNodeEnv() && error instanceof Error; | ||
const isRemixErrorResponse = isErrorResponse(error); | ||
// Server-side errors apart from `ErrorResponse`s also appear here without their stacktraces. | ||
// So, we only capture: | ||
// 1. `ErrorResponse`s | ||
// 2. Client-side runtime errors here, | ||
// And other server - side errors in `handleError` function where stacktraces are available. | ||
if (isRemixErrorResponse || isClientSideRuntimeError) { | ||
const eventData = isRemixErrorResponse | ||
? { | ||
function: 'ErrorResponse', | ||
...error.data, | ||
} | ||
: { | ||
function: 'ReactError', | ||
}; | ||
|
||
withScope(scope => { | ||
scope.addEventProcessor(event => { | ||
addExceptionMechanism(event, { | ||
type: 'instrument', | ||
handled: true, | ||
data: eventData, | ||
}); | ||
return event; | ||
}); | ||
|
||
if (isRemixErrorResponse) { | ||
if (isString(error.data)) { | ||
captureException(error.data); | ||
} else if (error.statusText) { | ||
captureException(error.statusText); | ||
} else { | ||
captureException(error); | ||
} | ||
} else { | ||
captureException(error); | ||
} | ||
}); | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { GLOBAL_OBJ } from '@sentry/utils'; | ||
|
||
import type { FutureConfig, ServerBuild } from './vendor/types'; | ||
|
||
export type EnhancedGlobal = typeof GLOBAL_OBJ & { | ||
__remixContext?: { | ||
future?: FutureConfig; | ||
}; | ||
}; | ||
|
||
/** | ||
* Get the future flags from the Remix browser context | ||
* | ||
* @returns The future flags | ||
*/ | ||
export function getFutureFlagsBrowser(): FutureConfig | undefined { | ||
const window = GLOBAL_OBJ as EnhancedGlobal; | ||
|
||
if (!window.__remixContext) { | ||
return; | ||
} | ||
|
||
return window.__remixContext.future; | ||
} | ||
|
||
/** | ||
* Get the future flags from the Remix server build | ||
* | ||
* @param build The Remix server build | ||
* | ||
* @returns The future flags | ||
*/ | ||
export function getFutureFlagsServer(build: ServerBuild): FutureConfig | undefined { | ||
return build.future; | ||
} |
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
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
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
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
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
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
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
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
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
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.