-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Include potential statusCode for next.js Error return #33245
Conversation
types/next/index.d.ts
Outdated
@@ -52,7 +52,7 @@ declare namespace next { | |||
/** Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response */ | |||
jsonPageRes?: NodeResponse; | |||
/** Error object if any error is encountered during the rendering */ | |||
err?: Error; | |||
err?: Error & {statusCode? : number}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like that the type of err
is not Error
. Guessing it's a Response
object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within the next code base they use this createError
function from micro
:
const createError = (code, message, original) => {
const err = new Error(message);
err.statusCode = code;
err.originalError = original;
return err;
};
for the errors, tacking the status code onto the Error
object. They then suggest in the next docs when creating your own _error.js
page to rely on this statusCode
property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we go with this approach I'd suggest including originalError
for completeness as well
@rgbkrk Thank you for submitting this PR! 🔔 @Dru89 @brikou @jthegedus @resir014 @scottdj92 @joaovieira @ajliv - please review this PR in the next few days. Be sure to explicitly select If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead. |
@rgbkrk The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
@rgbkrk you can attach anything to your Error. Thus, I'd say this needs to be cast explicitly on your client, either to static getInitialProps(context: NextContext) {
const { err, res } = context;
const statusCode = res ? res.statusCode : err ? (err as any).statusCode : null;
return { statusCode };
} You can also override your interface MyError extends Error {
statusCode?: number;
originalError?: Error;
}
interface MyNextContext extends NextContext {
err?: MyError
}
static getInitialProps(context: MyNextContext) {
const { err, res } = context;
const statusCode = res ? res.statusCode : err ? err.statusCode : null;
return { statusCode };
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rgbkrk @joaovieira Since the err
property is only used in _error.tsx
as the documentation suggests, it might be better to limit the scope of this change to error.d.ts
instead. I think we're better off doing this:
// error.d.ts
export interface NextErrorContext extends NextContext {
err: DefaultErrorIProps
}
export default class Error<P = {}> extends React.Component<P & DefaultErrorIProps> {
static getInitialProps(context: NextErrorContext): Promise<DefaultErrorIProps> | DefaultErrorIProps;
}
@rgbkrk I added my change suggestion. My finger slipped so my message might have accidentally got cut short if you received your notifications via email. 😅 |
@resir014 I think we should be able to use |
Ah, you're right, |
Thank you all so much for digging into this to find the truth! |
A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped! |
@joaovieira @mohsen1 Do you have any remaining concerns? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rgbkrk as you pointed out the default Next.js error also has an originalError
prop. Could you add some tests as well?
@rgbkrk One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits or comments. Thank you! |
@rgbkrk I haven't seen anything from you in a while and this PR currently has problems that prevent it from being merged. The PR will be closed tomorrow if there aren't new commits to fix the issues. |
I'm going on paternity leave for a bit, so if anyone wants to pick up this PR (or even start a new one), I'd really appreciate it! |
@rgbkrk To keep things tidy, we have to close PRs that aren't mergeable but don't have activity from their author. No worries, though - please open a new PR if you'd like to continue with this change. Thank you! |
Please fill in this template.
npm run lint package-name
(ortsc
if notslint.json
is present).Select one of these and delete the others:
If changing an existing definition:
createError
frommicro
creates the errors withstatusCode
set: https://github.com/zeit/micro/blob/069f31fa506086cdbf0ce3364d16ab972e8cc200/lib/index.js