-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Typing issue with the custom _error
page in TypeScript
#5448
Comments
It looks like the typing issue can be fixed this way. CustomErrorComponent.getInitialProps = async (context) => {
const errorInitialProps = await NextErrorComponent.getInitialProps(context);
await Sentry.captureUnderscoreErrorException(errorInitialProps);
return NextErrorComponent.getInitialProps(context);
}; Not sure if this is expected though? |
@lobsterkatie could you take a look? |
The types check, but I would expect you wouldn't get the data you want on your sentry error in that case. What's happening here is we're running into microsoft/TypeScript#15300. The two solutions I can find are:
All things being equal, 1) is better, because it doesn't require any change on our users' part. I was reluctant to do it, because I expected it to break a case in which someone who still has the legacy const CustomErrorComponent = (props) => {
// people might still have this
Sentry.captureUnderscoreErrorException(props); // I would expect this to error in TS
console.log(props.stuff);
return <NextErrorComponent statusCode={props.statusCode} />;
};
CustomErrorComponent.getInitialProps = async (contextData) => {
// In case this is running in a serverless function, await this in order to give Sentry
// time to send the error before the lambda exits
await Sentry.captureUnderscoreErrorException(contextData);
// This will contain the status code of the response
const result = NextErrorComponent.getInitialProps(contextData);
// IRL this would probably come from a 'fetch' call
result.stuff = "things";
return result;
}; As it turns out, though, in order to do that you have to jump through enough hoops that our function doesn't seem to mind: type GetInitialPropsResult = ErrorProps & { stuff?: string };
const CustomErrorComponent: NextPage<GetInitialPropsResult> = (
props: GetInitialPropsResult
) => {
// people might still have this
Sentry.captureUnderscoreErrorException(props); // I would expect this to error in TS
console.log(props.stuff);
return <NextErrorComponent statusCode={props.statusCode} />;
};
CustomErrorComponent.getInitialProps = async (
contextData: NextPageContext
): Promise<GetInitialPropsResult> => {
// In case this is running in a serverless function, await this in order to give Sentry
// time to send the error before the lambda exits
await Sentry.captureUnderscoreErrorException(contextData);
// This will contain the status code of the response
const result = NextErrorComponent.getInitialProps(
contextData
) as GetInitialPropsResult;
// IRL this would probably come from a 'fetch' call
result.stuff = "things";
return result;
}; I will admit I'm still confused why it doesn't mind, but it seems not to mind - our SDK builds, my test app's |
…n` argument type (#5463) This removes the index signature in the `ContextOrProps` type in our nextjs `_error` helper function, `captureUnderscoreErrorException`, as it's causing build errors for people writing their `_error` page in TS. Note: The problem I anticipated[1], which led me to add it in the first place, hasn't materialized in my testing. Fixes #5448. [1] #5448 (comment)
ran into this error today @lobsterkatie can you reopen? |
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
@sentry/nextjs
SDK Version
7.7.0
Framework Version
12.2.2
Link to Sentry event
No response
Steps to Reproduce
_error.js
custom page to_error.tsx
ErrorProps
type fromnext/error
to theCustomErrorComponent
Expected Result
No typing issue with TypeScript.
Actual Result
The text was updated successfully, but these errors were encountered: