-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass prod error messages directly to constructor (#17063)
* Remove "Invariant Violation" from dev errors When I made the change to compile `invariant` to throw expressions, I left a small runtime to set the error's `name` property to "Invariant Violation" to maintain the existing behavior. I think we can remove it. The argument for keeping it is to preserve continuity in error logs, but this only affects development errors, anyway: production error messages are replaced with error codes. * Pass prod error messages directly to constructor Updates the `invariant` transform to pass an error message string directly to the Error constructor, instead of mutating the message property. Turns this code: ```js invariant(condition, 'A %s message that contains %s', adj, noun); ``` into this: ```js if (!condition) { throw Error( __DEV__ ? `A ${adj} message that contains ${noun}` : formatProdErrorMessage(ERR_CODE, adj, noun) ); } ```
- Loading branch information
Showing
7 changed files
with
81 additions
and
131 deletions.
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 was deleted.
Oops, something went wrong.
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
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
54 changes: 19 additions & 35 deletions
54
scripts/error-codes/__tests__/__snapshots__/transform-error-messages.js.snap
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 |
---|---|---|
@@ -1,76 +1,60 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`error transform should correctly transform invariants that are not in the error codes map 1`] = ` | ||
"import _ReactError from \\"shared/ReactError\\"; | ||
import invariant from 'shared/invariant'; | ||
"import invariant from 'shared/invariant'; | ||
/*FIXME (minify-errors-in-prod): Unminified error message in production build!*/ | ||
(function () { | ||
if (!condition) { | ||
throw _ReactError(Error(\\"This is not a real error message.\\")); | ||
} | ||
})();" | ||
if (!condition) { | ||
throw Error(\\"This is not a real error message.\\"); | ||
}" | ||
`; | ||
|
||
exports[`error transform should handle escaped characters 1`] = ` | ||
"import _ReactError from \\"shared/ReactError\\"; | ||
import invariant from 'shared/invariant'; | ||
"import invariant from 'shared/invariant'; | ||
/*FIXME (minify-errors-in-prod): Unminified error message in production build!*/ | ||
(function () { | ||
if (!condition) { | ||
throw _ReactError(Error(\\"What's up?\\")); | ||
} | ||
})();" | ||
if (!condition) { | ||
throw Error(\\"What's up?\\"); | ||
}" | ||
`; | ||
|
||
exports[`error transform should replace simple invariant calls 1`] = ` | ||
"import _ReactErrorProd from \\"shared/ReactErrorProd\\"; | ||
import _ReactError from \\"shared/ReactError\\"; | ||
"import _formatProdErrorMessage from \\"shared/formatProdErrorMessage\\"; | ||
import invariant from 'shared/invariant'; | ||
if (!condition) { | ||
if (__DEV__) { | ||
throw _ReactError(Error(\\"Do not override existing functions.\\")); | ||
} else { | ||
throw _ReactErrorProd(Error(16)); | ||
{ | ||
throw Error(__DEV__ ? \\"Do not override existing functions.\\" : _formatProdErrorMessage(16)); | ||
} | ||
}" | ||
`; | ||
|
||
exports[`error transform should support invariant calls with a concatenated template string and args 1`] = ` | ||
"import _ReactErrorProd from \\"shared/ReactErrorProd\\"; | ||
import _ReactError from \\"shared/ReactError\\"; | ||
"import _formatProdErrorMessage from \\"shared/formatProdErrorMessage\\"; | ||
import invariant from 'shared/invariant'; | ||
if (!condition) { | ||
if (__DEV__) { | ||
throw _ReactError(Error(\\"Expected a component class, got \\" + Foo + \\".\\" + Bar)); | ||
} else { | ||
throw _ReactErrorProd(Error(18), Foo, Bar); | ||
{ | ||
throw Error(__DEV__ ? \\"Expected a component class, got \\" + Foo + \\".\\" + Bar : _formatProdErrorMessage(18, Foo, Bar)); | ||
} | ||
}" | ||
`; | ||
|
||
exports[`error transform should support invariant calls with args 1`] = ` | ||
"import _ReactErrorProd from \\"shared/ReactErrorProd\\"; | ||
import _ReactError from \\"shared/ReactError\\"; | ||
"import _formatProdErrorMessage from \\"shared/formatProdErrorMessage\\"; | ||
import invariant from 'shared/invariant'; | ||
if (!condition) { | ||
if (__DEV__) { | ||
throw _ReactError(Error(\\"Expected \\" + foo + \\" target to be an array; got \\" + bar)); | ||
} else { | ||
throw _ReactErrorProd(Error(7), foo, bar); | ||
{ | ||
throw Error(__DEV__ ? \\"Expected \\" + foo + \\" target to be an array; got \\" + bar : _formatProdErrorMessage(7, foo, bar)); | ||
} | ||
}" | ||
`; | ||
|
||
exports[`error transform should support noMinify option 1`] = ` | ||
"import _ReactError from \\"shared/ReactError\\"; | ||
import invariant from 'shared/invariant'; | ||
"import invariant from 'shared/invariant'; | ||
if (!condition) { | ||
throw _ReactError(Error(\\"Do not override existing functions.\\")); | ||
throw Error(\\"Do not override existing functions.\\"); | ||
}" | ||
`; |
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