-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Assign unique error codes to production invariant errors. #4521
Conversation
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.
Great stuff - thanks @benjamn!
// location), which makes it much easier to trace production | ||
// errors back to the unminified code where they were thrown, | ||
// where the full error string can be found. See #4519. | ||
errorCodes: true, |
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.
This is great! Would it be possible for the error codes to be there in both the production and development versions?
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.
The error codes will be present in Apollo Client library code in development, since the unminified code looks like this:
process.env.NODE_ENV === 'production'
? invariant(condition, <error code>)
: invariant(condition, message)
The codes won't be automatically included in the error messages in development, though. I think this is a reasonable balance, since these error codes are subject to change, so we don't want to commit to specific numbers for specific errors. The goal of this PR is just to make sure it's possible to make sense of production errors.
Inspired by #4519. Implemented and explained by apollographql/invariant-packages#1.
3081ba2
to
d668ca4
Compare
@benjamn is there a way to decode these numbers without knowing the apollo-client codebase?
I don't even know where to start. |
@skuridin best way I found so far is running:
Then try and guess which package actually raised the invariant error (error codes are unique per package, not globally). May I ask what's the rationale behind this btw? (hiding error messages in production). Doesn't seem like something that actually needs to be kept secret from the client? And doesn't seem to be improving performance either. |
@rshk - This doesn't work all of the time. I recently tried with a development build, looking for a code 14. Nothing. Skipped right over 14. There was a 13 and 15, but no 14 like we're seeing in production. 😦 |
Did you try searching for both @rshk This has been one of the more effective ways of reducing bundle sizes, since those error strings can be long and they do not minify. |
@benjamn - THERE IT IS! 🤦♂ You are right. Thank you sir. I'll delete my comment on the other thread. To note everyone, I was missing my error, but this resolved that:
|
Any possibility to determine the error type dynamically? Since this error is not really a "error" I don't want it to appear in logs so I want to put fetchMore in "try...catch" and suppress there, but I don't want to accidentally suppress some other error. I can not do So my question whether it is possible to know that code "17" corresponds to "ObservableQuery with this id doesn't exist" in code and not with |
It's really frustrating. Invariant error codes can be repeated in packages, so without sourcemaps we can't tell which error is actually happening.
And why the URL doesn't point to some place useful like a doc or something? It's not helpful at all to point to invariant-packages repo. |
@Grohden There's an explanation of error codes in the |
@benjamn I mean 'doc or something' like a place where the codes are documented (well, technically, they're documented on the code, but multiple packages with same code makes this unhelpful)... anyway, invariant could point which package is triggering this (force some the package to inform it maybe?), then the grep multiple return wouldn't be a problem. Maybe add an anchor (https://github.com/apollographql/invariant-packages#error-codes) and document a way about how to find the error codes (eg: grep) could make things better... |
@benjamn - This is definitely falling apart as a method. Deliberate codes are needed here. Invariant codes are no longer matching up, even when using the same method. If your error codes only work with source maps live, you have a problem. To provide a little more context, I have a fixed version of Apollo Client, but when I build locally vs when I build in an environment like AWS Codebuild, things stop matching. One environment matches the error up with code 19, and one environment may match the error up with code 10. As a result, the only way I can truly match up an error in ApolloClient is to reproduce the error in dev. It's very disappointing. |
@benjamn - Nevermind, I'm an idiot. I'm looking at your invariant stuff, and it seems pretty much 1:1 with how react does stuff. I'm just flustered and confused. Somehow I was getting an error code that matched up with 19 in my staging environment, something about an invalid string or something having to do with SSR. I built locally, assuming everything was going to be the same since they're on the same version. However, when I ran it locally in dev, the error was about using an invalid fetchPolicy for mutations, and I checked where this was in the codebase, and it matched up with error code 10. I don't know why something like that would occur, and it frankly scares the willies out of me. I need to get my source maps working again >_< |
When an invariant fails in production, a cryptic numeric error of the form `Invariant Violation: 42` is thrown, with a reference to the error codes section of invariant-packages README.md: https://github.com/apollographql/invariant-packages#error-codes This vague guidance has not proven adequate in many cases, to say the least: see apollographql/invariant-packages#18, apollographql/invariant-packages#19, #6604, However, this vague guidance (which was intended to suggest searching your installed node_modules/@apollo/client package for the error code) has not proven adequate in many cases, to say the least: see apollographql/invariant-packages#18, apollographql/invariant-packages#19, #6604, #5730, #5291, and #5975, to cite just a few of the many issues we've seen since #4521. Using error codes instead of error strings remains important for production bundle sizes, but I think we can make it substantially easier to look up the error string corresponding to each error code, by generating a single file containing all the invariant error codes for each @apollo/client release. Starting with Apollo Client 3.1.0, this manifest file can be found in @apollo/client/invariantErrorCodes.js (using an npm/yarn-installed copy of @apollo/client, since this file is generated in the ./dist directory, not checked into the repository). The file contains an explanatory comment, the @apollo/client version, and a sequential map from error numbers to the { file, node } responsible for the error.
…llographql#6665) When an invariant fails in production, a cryptic numeric error of the form `Invariant Violation: 42` is thrown, with a reference to the error codes section of invariant-packages README.md: https://github.com/apollographql/invariant-packages#error-codes This vague guidance has not proven adequate in many cases, to say the least: see apollographql/invariant-packages#18, apollographql/invariant-packages#19, apollographql#6604, However, this vague guidance (which was intended to suggest searching your installed node_modules/@apollo/client package for the error code) has not proven adequate in many cases, to say the least: see apollographql/invariant-packages#18, apollographql/invariant-packages#19, apollographql#6604, apollographql#5730, apollographql#5291, and apollographql#5975, to cite just a few of the many issues we've seen since apollographql#4521. Using error codes instead of error strings remains important for production bundle sizes, but I think we can make it substantially easier to look up the error string corresponding to each error code, by generating a single file containing all the invariant error codes for each @apollo/client release. Starting with Apollo Client 3.1.0, this manifest file can be found in @apollo/client/invariantErrorCodes.js (using an npm/yarn-installed copy of @apollo/client, since this file is generated in the ./dist directory, not checked into the repository). The file contains an explanatory comment, the @apollo/client version, and a sequential map from error numbers to the { file, node } responsible for the error.
Should help address #4519.
Implemented and explained by apollographql/invariant-packages#1.