GeezerVison is showing github.com — Pass prod error messages directly to constructor (#17063) · react/react@75955bf
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/__tests__/ReactNativeError-test.internal.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,7 +40,7 @@ describe('ReactNativeError', () => { | 40 | 40 | throw new Error(e.toString()); | 41 | 41 | } | 42 | 42 | }).toThrow( | 43 | | - 'Invariant Violation: View config getter callback for component `View` must be a function (received `null`)', | | 43 | + 'View config getter callback for component `View` must be a function (received `null`)', | 44 | 44 | ); | 45 | 45 | }); | 46 | 46 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactError.js -18Lines changed: 0 additions & 18 deletions Collapse file Copy file name to clipboardExpand all lines: packages/shared/__tests__/ReactError-test.internal.js +1-9Lines changed: 1 addition & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,15 +37,7 @@ describe('ReactError', () => { | 37 | 37 | }); | 38 | 38 |
| 39 | 39 | if (__DEV__) { | 40 | | - it('should throw errors whose name is "Invariant Violation"', () => { | 41 | | - let error; | 42 | | - try { | 43 | | - React.useState(); | 44 | | - } catch (e) { | 45 | | - error = e; | 46 | | - } | 47 | | - expect(error.name).toEqual('Invariant Violation'); | 48 | | - }); | | 40 | + it("empty test so Jest doesn't complain", () => {}); | 49 | 41 | } else { | 50 | 42 | it('should error with minified error code', () => { | 51 | 43 | expect(() => ReactDOM.render('Hi', null)).toThrowError( | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/__tests__/ReactErrorProd-test.internal.js +5-11Lines changed: 5 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 | 'use strict'; | 10 | 10 |
| 11 | | -let ReactErrorProd; | | 11 | +let formatProdErrorMessage; | 12 | 12 |
| 13 | 13 | describe('ReactErrorProd', () => { | 14 | 14 | let globalErrorMock; | @@ -25,7 +25,7 @@ describe('ReactErrorProd', () => { | 25 | 25 | expect(typeof global.Error).toBe('function'); | 26 | 26 | } | 27 | 27 | jest.resetModules(); | 28 | | - ReactErrorProd = require('shared/ReactErrorProd').default; | | 28 | + formatProdErrorMessage = require('shared/formatProdErrorMessage').default; | 29 | 29 | }); | 30 | 30 |
| 31 | 31 | afterEach(() => { | @@ -35,27 +35,21 @@ describe('ReactErrorProd', () => { | 35 | 35 | }); | 36 | 36 |
| 37 | 37 | it('should throw with the correct number of `%s`s in the URL', () => { | 38 | | - expect(function() { | 39 | | - throw ReactErrorProd(Error(124), 'foo', 'bar'); | 40 | | - }).toThrowError( | | 38 | + expect(formatProdErrorMessage(124, 'foo', 'bar')).toEqual( | 41 | 39 | 'Minified React error #124; visit ' + | 42 | 40 | 'https://reactjs.org/docs/error-decoder.html?invariant=124&args[]=foo&args[]=bar' + | 43 | 41 | ' for the full message or use the non-minified dev environment' + | 44 | 42 | ' for full errors and additional helpful warnings.', | 45 | 43 | ); | 46 | 44 |
| 47 | | - expect(function() { | 48 | | - throw ReactErrorProd(Error(20)); | 49 | | - }).toThrowError( | | 45 | + expect(formatProdErrorMessage(20)).toEqual( | 50 | 46 | 'Minified React error #20; visit ' + | 51 | 47 | 'https://reactjs.org/docs/error-decoder.html?invariant=20' + | 52 | 48 | ' for the full message or use the non-minified dev environment' + | 53 | 49 | ' for full errors and additional helpful warnings.', | 54 | 50 | ); | 55 | 51 |
| 56 | | - expect(function() { | 57 | | - throw ReactErrorProd(Error(77), '<div>', '&?bar'); | 58 | | - }).toThrowError( | | 52 | + expect(formatProdErrorMessage(77, '<div>', '&?bar')).toEqual( | 59 | 53 | 'Minified React error #77; visit ' + | 60 | 54 | 'https://reactjs.org/docs/error-decoder.html?invariant=77&args[]=%3Cdiv%3E&args[]=%26%3Fbar' + | 61 | 55 | ' for the full message or use the non-minified dev environment' + | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/formatProdErrorMessage.js +7-8Lines changed: 7 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | */ | 8 | 8 |
| 9 | 9 | // Do not require this module directly! Use normal `invariant` calls with | 10 | | -// template literal strings. The messages will be converted to ReactError during | 11 | | -// build, and in production they will be minified. | | 10 | +// template literal strings. The messages will be replaced with error codes | | 11 | +// during build. | 12 | 12 |
| 13 | | -function ReactErrorProd(error) { | 14 | | - const code = error.message; | | 13 | +function formatProdErrorMessage(code) { | 15 | 14 | let url = 'https://reactjs.org/docs/error-decoder.html?invariant=' + code; | 16 | 15 | for (let i = 1; i < arguments.length; i++) { | 17 | 16 | url += '&args[]=' + encodeURIComponent(arguments[i]); | 18 | 17 | } | 19 | | - error.message = | | 18 | + return ( | 20 | 19 | `Minified React error #${code}; visit ${url} for the full message or ` + | 21 | 20 | 'use the non-minified dev environment for full errors and additional ' + | 22 | | - 'helpful warnings. '; | 23 | | - return error; | | 21 | + 'helpful warnings.' | | 22 | + ); | 24 | 23 | } | 25 | 24 |
| 26 | | -export default ReactErrorProd; | | 25 | +export default formatProdErrorMessage; |
Collapse file Copy file name to clipboard +19-35Lines changed: 19 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP | 2 | 2 |
| 3 | 3 | exports[`error transform should correctly transform invariants that are not in the error codes map 1`] = ` | 4 | | -"import _ReactError from \\"shared/ReactError\\"; | 5 | | -import invariant from 'shared/invariant'; | | 4 | +"import invariant from 'shared/invariant'; | 6 | 5 |
| 7 | 6 | /*FIXME (minify-errors-in-prod): Unminified error message in production build!*/ | 8 | | -(function () { | 9 | | - if (!condition) { | 10 | | - throw _ReactError(Error(\\"This is not a real error message.\\")); | 11 | | - } | 12 | | -})();" | | 7 | +if (!condition) { | | 8 | + throw Error(\\"This is not a real error message.\\"); | | 9 | +}" | 13 | 10 | `; | 14 | 11 |
| 15 | 12 | exports[`error transform should handle escaped characters 1`] = ` | 16 | | -"import _ReactError from \\"shared/ReactError\\"; | 17 | | -import invariant from 'shared/invariant'; | | 13 | +"import invariant from 'shared/invariant'; | 18 | 14 |
| 19 | 15 | /*FIXME (minify-errors-in-prod): Unminified error message in production build!*/ | 20 | | -(function () { | 21 | | - if (!condition) { | 22 | | - throw _ReactError(Error(\\"What's up?\\")); | 23 | | - } | 24 | | -})();" | | 16 | +if (!condition) { | | 17 | + throw Error(\\"What's up?\\"); | | 18 | +}" | 25 | 19 | `; | 26 | 20 |
| 27 | 21 | exports[`error transform should replace simple invariant calls 1`] = ` | 28 | | -"import _ReactErrorProd from \\"shared/ReactErrorProd\\"; | 29 | | -import _ReactError from \\"shared/ReactError\\"; | | 22 | +"import _formatProdErrorMessage from \\"shared/formatProdErrorMessage\\"; | 30 | 23 | import invariant from 'shared/invariant'; | 31 | 24 |
| 32 | 25 | if (!condition) { | 33 | | - if (__DEV__) { | 34 | | - throw _ReactError(Error(\\"Do not override existing functions.\\")); | 35 | | - } else { | 36 | | - throw _ReactErrorProd(Error(16)); | | 26 | + { | | 27 | + throw Error(__DEV__ ? \\"Do not override existing functions.\\" : _formatProdErrorMessage(16)); | 37 | 28 | } | 38 | 29 | }" | 39 | 30 | `; | 40 | 31 |
| 41 | 32 | exports[`error transform should support invariant calls with a concatenated template string and args 1`] = ` | 42 | | -"import _ReactErrorProd from \\"shared/ReactErrorProd\\"; | 43 | | -import _ReactError from \\"shared/ReactError\\"; | | 33 | +"import _formatProdErrorMessage from \\"shared/formatProdErrorMessage\\"; | 44 | 34 | import invariant from 'shared/invariant'; | 45 | 35 |
| 46 | 36 | if (!condition) { | 47 | | - if (__DEV__) { | 48 | | - throw _ReactError(Error(\\"Expected a component class, got \\" + Foo + \\".\\" + Bar)); | 49 | | - } else { | 50 | | - throw _ReactErrorProd(Error(18), Foo, Bar); | | 37 | + { | | 38 | + throw Error(__DEV__ ? \\"Expected a component class, got \\" + Foo + \\".\\" + Bar : _formatProdErrorMessage(18, Foo, Bar)); | 51 | 39 | } | 52 | 40 | }" | 53 | 41 | `; | 54 | 42 |
| 55 | 43 | exports[`error transform should support invariant calls with args 1`] = ` | 56 | | -"import _ReactErrorProd from \\"shared/ReactErrorProd\\"; | 57 | | -import _ReactError from \\"shared/ReactError\\"; | | 44 | +"import _formatProdErrorMessage from \\"shared/formatProdErrorMessage\\"; | 58 | 45 | import invariant from 'shared/invariant'; | 59 | 46 |
| 60 | 47 | if (!condition) { | 61 | | - if (__DEV__) { | 62 | | - throw _ReactError(Error(\\"Expected \\" + foo + \\" target to be an array; got \\" + bar)); | 63 | | - } else { | 64 | | - throw _ReactErrorProd(Error(7), foo, bar); | | 48 | + { | | 49 | + throw Error(__DEV__ ? \\"Expected \\" + foo + \\" target to be an array; got \\" + bar : _formatProdErrorMessage(7, foo, bar)); | 65 | 50 | } | 66 | 51 | }" | 67 | 52 | `; | 68 | 53 |
| 69 | 54 | exports[`error transform should support noMinify option 1`] = ` | 70 | | -"import _ReactError from \\"shared/ReactError\\"; | 71 | | -import invariant from 'shared/invariant'; | | 55 | +"import invariant from 'shared/invariant'; | 72 | 56 |
| 73 | 57 | if (!condition) { | 74 | | - throw _ReactError(Error(\\"Do not override existing functions.\\")); | | 58 | + throw Error(\\"Do not override existing functions.\\"); | 75 | 59 | }" | 76 | 60 | `; |
Collapse file Copy file name to clipboardExpand all lines: scripts/error-codes/transform-error-messages.js +48-49Lines changed: 48 additions & 49 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,11 +29,11 @@ module.exports = function(babel) { | 29 | 29 | // into this: | 30 | 30 | // | 31 | 31 | // if (!condition) { | 32 | | - // if (__DEV__) { | 33 | | - // throw ReactError(Error(`A ${adj} message that contains ${noun}`)); | 34 | | - // } else { | 35 | | - // throw ReactErrorProd(Error(ERR_CODE), adj, noun); | 36 | | - // } | | 32 | + // throw Error( | | 33 | + // __DEV__ | | 34 | + // ? `A ${adj} message that contains ${noun}` | | 35 | + // : formatProdErrorMessage(ERR_CODE, adj, noun) | | 36 | + // ); | 37 | 37 | // } | 38 | 38 | // | 39 | 39 | // where ERR_CODE is an error code: a unique identifier (a number | @@ -46,22 +46,11 @@ module.exports = function(babel) { | 46 | 46 | .split('%s') | 47 | 47 | .map(raw => t.templateElement({raw, cooked: String.raw({raw})})); | 48 | 48 |
| 49 | | - const reactErrorIdentfier = helperModuleImports.addDefault( | 50 | | - path, | 51 | | - 'shared/ReactError', | 52 | | - { | 53 | | - nameHint: 'ReactError', | 54 | | - } | 55 | | - ); | 56 | | - | 57 | 49 | // Outputs: | 58 | | - // throw ReactError(Error(`A ${adj} message that contains ${noun}`)); | 59 | | - const devThrow = t.throwStatement( | 60 | | - t.callExpression(reactErrorIdentfier, [ | 61 | | - t.callExpression(t.identifier('Error'), [ | 62 | | - t.templateLiteral(errorMsgQuasis, errorMsgExpressions), | 63 | | - ]), | 64 | | - ]) | | 50 | + // `A ${adj} message that contains ${noun}`; | | 51 | + const devMessage = t.templateLiteral( | | 52 | + errorMsgQuasis, | | 53 | + errorMsgExpressions | 65 | 54 | ); | 66 | 55 |
| 67 | 56 | const parentStatementPath = path.parentPath; | @@ -77,12 +66,16 @@ module.exports = function(babel) { | 77 | 66 | // | 78 | 67 | // Outputs: | 79 | 68 | // if (!condition) { | 80 | | - // throw ReactError(Error(`A ${adj} message that contains ${noun}`)); | | 69 | + // throw Error(`A ${adj} message that contains ${noun}`); | 81 | 70 | // } | 82 | 71 | parentStatementPath.replaceWith( | 83 | 72 | t.ifStatement( | 84 | 73 | t.unaryExpression('!', condition), | 85 | | - t.blockStatement([devThrow]) | | 74 | + t.blockStatement([ | | 75 | + t.throwStatement( | | 76 | + t.callExpression(t.identifier('Error'), [devMessage]) | | 77 | + ), | | 78 | + ]) | 86 | 79 | ) | 87 | 80 | ); | 88 | 81 | return; | @@ -104,15 +97,19 @@ module.exports = function(babel) { | 104 | 97 | // Outputs: | 105 | 98 | // /* FIXME (minify-errors-in-prod): Unminified error message in production build! */ | 106 | 99 | // if (!condition) { | 107 | | - // throw ReactError(Error(`A ${adj} message that contains ${noun}`)); | | 100 | + // throw Error(`A ${adj} message that contains ${noun}`); | 108 | 101 | // } | 109 | | - path.replaceWith( | | 102 | + parentStatementPath.replaceWith( | 110 | 103 | t.ifStatement( | 111 | 104 | t.unaryExpression('!', condition), | 112 | | - t.blockStatement([devThrow]) | | 105 | + t.blockStatement([ | | 106 | + t.throwStatement( | | 107 | + t.callExpression(t.identifier('Error'), [devMessage]) | | 108 | + ), | | 109 | + ]) | 113 | 110 | ) | 114 | 111 | ); | 115 | | - path.addComment( | | 112 | + parentStatementPath.addComment( | 116 | 113 | 'leading', | 117 | 114 | 'FIXME (minify-errors-in-prod): Unminified error message in production build!' | 118 | 115 | ); | @@ -121,40 +118,42 @@ module.exports = function(babel) { | 121 | 118 | prodErrorId = parseInt(prodErrorId, 10); | 122 | 119 |
| 123 | 120 | // Import ReactErrorProd | 124 | | - const reactErrorProdIdentfier = helperModuleImports.addDefault( | | 121 | + const formatProdErrorMessageIdentifier = helperModuleImports.addDefault( | 125 | 122 | path, | 126 | | - 'shared/ReactErrorProd', | 127 | | - {nameHint: 'ReactErrorProd'} | | 123 | + 'shared/formatProdErrorMessage', | | 124 | + {nameHint: 'formatProdErrorMessage'} | 128 | 125 | ); | 129 | 126 |
| 130 | 127 | // Outputs: | 131 | | - // throw ReactErrorProd(Error(ERR_CODE), adj, noun); | 132 | | - const prodThrow = t.throwStatement( | 133 | | - t.callExpression(reactErrorProdIdentfier, [ | 134 | | - t.callExpression(t.identifier('Error'), [ | 135 | | - t.numericLiteral(prodErrorId), | 136 | | - ]), | 137 | | - ...errorMsgExpressions, | 138 | | - ]) | | 128 | + // formatProdErrorMessage(ERR_CODE, adj, noun); | | 129 | + const prodMessage = t.callExpression( | | 130 | + formatProdErrorMessageIdentifier, | | 131 | + [t.numericLiteral(prodErrorId), ...errorMsgExpressions] | 139 | 132 | ); | 140 | 133 |
| 141 | 134 | // Outputs: | 142 | | - // if (!condition) { | 143 | | - // if (__DEV__) { | 144 | | - // throw ReactError(Error(`A ${adj} message that contains ${noun}`)); | 145 | | - // } else { | 146 | | - // throw ReactErrorProd(Error(ERR_CODE), adj, noun); | 147 | | - // } | 148 | | - // } | | 135 | + // if (!condition) { | | 136 | + // throw Error( | | 137 | + // __DEV__ | | 138 | + // ? `A ${adj} message that contains ${noun}` | | 139 | + // : formatProdErrorMessage(ERR_CODE, adj, noun) | | 140 | + // ); | | 141 | + // } | 149 | 142 | parentStatementPath.replaceWith( | 150 | 143 | t.ifStatement( | 151 | 144 | t.unaryExpression('!', condition), | 152 | 145 | t.blockStatement([ | 153 | | - t.ifStatement( | 154 | | - DEV_EXPRESSION, | 155 | | - t.blockStatement([devThrow]), | 156 | | - t.blockStatement([prodThrow]) | 157 | | - ), | | 146 | + t.blockStatement([ | | 147 | + t.throwStatement( | | 148 | + t.callExpression(t.identifier('Error'), [ | | 149 | + t.conditionalExpression( | | 150 | + DEV_EXPRESSION, | | 151 | + devMessage, | | 152 | + prodMessage | | 153 | + ), | | 154 | + ]) | | 155 | + ), | | 156 | + ]), | 158 | 157 | ]) | 159 | 158 | ) | 160 | 159 | ); | |
|