Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

rgbkrk
Copy link

@rgbkrk rgbkrk commented Feb 20, 2019

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If changing an existing definition:

@@ -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};
Copy link
Contributor

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?

Copy link
Author

@rgbkrk rgbkrk Feb 20, 2019

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.

Copy link
Contributor

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

@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Feb 20, 2019
@typescript-bot
Copy link
Contributor

typescript-bot commented Feb 20, 2019

@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 Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@typescript-bot
Copy link
Contributor

typescript-bot commented Feb 20, 2019

@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!

@joaovieira
Copy link
Contributor

joaovieira commented Feb 21, 2019

@rgbkrk you can attach anything to your Error. statusCode is just an example that Next.js uses in their default server implementation. Same applies for originalError apparently. I, for example, have a custom Express.js server and can attach anything to it.

Thus, I'd say this needs to be cast explicitly on your client, either to any or to your custom error type:

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 NextContext type:

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 };
}

Copy link
Contributor

@resir014 resir014 left a 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;
}

@resir014
Copy link
Contributor

@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. 😅

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Feb 21, 2019
@joaovieira
Copy link
Contributor

joaovieira commented Feb 21, 2019

@resir014 I think we should be able to use err.statusCode in any page, page HOC, or _app. It's the same error, same context. No?

@resir014
Copy link
Contributor

I think we should be able to use err.statusCode in any page, page HOC, or _app. It's the same error, same context. No?

Ah, you're right, err is the same on every page. I stand corrected. @joaovieira

@typescript-bot typescript-bot added the Owner Approved A listed owner of this package signed off on the pull request. label Feb 21, 2019
@rgbkrk
Copy link
Author

rgbkrk commented Feb 21, 2019

Thank you all so much for digging into this to find the truth!

@typescript-bot
Copy link
Contributor

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!

@amcasey
Copy link
Contributor

amcasey commented Feb 22, 2019

@joaovieira @mohsen1 Do you have any remaining concerns?

Copy link
Contributor

@joaovieira joaovieira left a 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?

@typescript-bot typescript-bot removed Owner Approved A listed owner of this package signed off on the pull request. Merge:Express labels Feb 22, 2019
@typescript-bot
Copy link
Contributor

@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!

@typescript-bot
Copy link
Contributor

@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.

@rgbkrk
Copy link
Author

rgbkrk commented Feb 28, 2019

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!

@typescript-bot typescript-bot added the Abandoned This PR had no activity for a long time, and is considered abandoned label Mar 1, 2019
@typescript-bot
Copy link
Contributor

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Abandoned This PR had no activity for a long time, and is considered abandoned Popular package This PR affects a popular package (as counted by NPM download counts). Revision needed This PR needs code changes before it can be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants