From d6c9c5ce04ea59da9016242865bfe8a8f3dbc9e0 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 19 Jun 2018 15:46:13 +0100 Subject: [PATCH] Preserve "Invariant Violation" name --- checkPropTypes.js | 4 +++- factoryWithThrowingShims.js | 4 +++- factoryWithTypeCheckers.js | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/checkPropTypes.js b/checkPropTypes.js index 7c06d4e..05403dc 100644 --- a/checkPropTypes.js +++ b/checkPropTypes.js @@ -50,10 +50,12 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack) { // This is intentionally an invariant that gets caught. It's the same // behavior as without this statement except with a better message. if (typeof typeSpecs[typeSpecName] !== 'function') { - throw new Error( + var err = Error( (componentName || 'React class') + ': ' + location + ' type `' + typeSpecName + '` is invalid; ' + 'it must be a function, usually from the `prop-types` package, but received `' + typeof typeSpecs[typeSpecName] + '`.' ); + err.name = 'Invariant Violation'; + throw err; } error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret); } catch (ex) { diff --git a/factoryWithThrowingShims.js b/factoryWithThrowingShims.js index 2ddf2d9..79b60f2 100644 --- a/factoryWithThrowingShims.js +++ b/factoryWithThrowingShims.js @@ -17,11 +17,13 @@ module.exports = function() { // It is still safe when called from React. return; } - throw new Error( + var err = new Error( 'Calling PropTypes validators directly is not supported by the `prop-types` package. ' + 'Use PropTypes.checkPropTypes() to call them. ' + 'Read more at http://fb.me/use-check-prop-types' ); + err.name = 'Invariant Violation'; + throw err; }; shim.isRequired = shim; function getShim() { diff --git a/factoryWithTypeCheckers.js b/factoryWithTypeCheckers.js index c4362a5..c41e3bb 100644 --- a/factoryWithTypeCheckers.js +++ b/factoryWithTypeCheckers.js @@ -175,11 +175,13 @@ module.exports = function(isValidElement, throwOnDirectAccess) { if (secret !== ReactPropTypesSecret) { if (throwOnDirectAccess) { // New behavior only for users of `prop-types` package - throw new Error( + var err = new Error( 'Calling PropTypes validators directly is not supported by the `prop-types` package. ' + 'Use `PropTypes.checkPropTypes()` to call them. ' + 'Read more at http://fb.me/use-check-prop-types' ); + err.name = 'Invariant Violation'; + throw err; } else if (process.env.NODE_ENV !== 'production' && typeof console !== 'undefined') { // Old behavior for people using React.PropTypes var cacheKey = componentName + ':' + propName;