-
-
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(nextjs): Instrument SSR page components
- Loading branch information
Showing
4 changed files
with
74 additions
and
1 deletion.
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
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,66 @@ | ||
import { captureException } from '@sentry/core'; | ||
import { addExceptionMechanism } from '@sentry/utils'; | ||
|
||
interface FunctionComponent { | ||
(...args: unknown[]): unknown; | ||
} | ||
|
||
interface ClassComponent { | ||
new (...args: unknown[]): { | ||
render(...args: unknown[]): unknown; | ||
}; | ||
} | ||
|
||
function isReactClassComponent(target: unknown): target is ClassComponent { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
return typeof target === 'function' && target?.prototype?.isReactComponent; | ||
} | ||
|
||
/** | ||
* Wraps a page component with Sentry error instrumentation. | ||
*/ | ||
export function wrapPageComponentWithSentry(pageComponent: FunctionComponent | ClassComponent): unknown { | ||
if (isReactClassComponent(pageComponent)) { | ||
return class SentryWrappedPageComponent extends pageComponent { | ||
public render(...args: unknown[]): unknown { | ||
try { | ||
return super.render(...args); | ||
} catch (e) { | ||
captureException(e, scope => { | ||
scope.addEventProcessor(event => { | ||
addExceptionMechanism(event, { | ||
handled: false, | ||
}); | ||
return event; | ||
}); | ||
|
||
return scope; | ||
}); | ||
throw e; | ||
} | ||
} | ||
}; | ||
} else if (typeof pageComponent === 'function') { | ||
return new Proxy(pageComponent, { | ||
apply(target, thisArg, argArray) { | ||
try { | ||
return target.apply(thisArg, argArray); | ||
} catch (e) { | ||
captureException(e, scope => { | ||
scope.addEventProcessor(event => { | ||
addExceptionMechanism(event, { | ||
handled: false, | ||
}); | ||
return event; | ||
}); | ||
|
||
return scope; | ||
}); | ||
throw e; | ||
} | ||
}, | ||
}); | ||
} else { | ||
return pageComponent; | ||
} | ||
} |
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