Skip to content

Commit

Permalink
checkPropTypes: add flag to throw the error instead of logging
Browse files Browse the repository at this point in the history
Adds a `checkPropTypeWithErrors` convenience function, which calls
`checkPropTypes` with `throwErrors` set to true

Fixes facebook#34
  • Loading branch information
rufman committed May 18, 2017
1 parent f2cf875 commit 92c745e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
25 changes: 25 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Expand Up @@ -146,6 +146,31 @@ describe('PropTypesDevelopmentStandalone', () => {
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
expect(returnValue).toBe(undefined);
});

it('throws an error if `throwError` is set', () => {
const error = 'some error';
const propTypes = {
foo(props, propName, componentName) {
throw new Error(error);
},
};
const props = {foo: 'foo'};

let returnValue;
const checkFn = () => {
returnValue = PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
true,
);
};

expect(checkFn).toThrow(error);
expect(returnValue).toBe(undefined);
});
});

describe('Primitive Types', () => {
Expand Down
8 changes: 6 additions & 2 deletions checkPropTypes.js
Expand Up @@ -27,7 +27,7 @@ if (process.env.NODE_ENV !== 'production') {
* @param {?Function} getStack Returns the component stack.
* @private
*/
function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
function checkPropTypes(typeSpecs, values, location, componentName, getStack, throwError) {
if (process.env.NODE_ENV !== 'production') {
for (var typeSpecName in typeSpecs) {
if (typeSpecs.hasOwnProperty(typeSpecName)) {
Expand All @@ -51,7 +51,11 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack) {

var stack = getStack ? getStack() : '';

warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
if (throwError) {
throw error;
} else {
warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions checkPropTypesWithErrors.js
@@ -0,0 +1,11 @@
'use strict';

if (process.env.NODE_ENV !== 'production') {
var checkPropTypes = require('./checkPropTypes');
}

function checkPropTypesWithErrors(typeSpecs, values, location, componentName, getStack) {
checkPropTypes(typeSpecs, values, location, componentName, getStack, true);
}

module.exports = checkPropTypesWithErrors;
1 change: 1 addition & 0 deletions factoryWithThrowingShims.js
Expand Up @@ -53,6 +53,7 @@ module.exports = function() {
};

ReactPropTypes.checkPropTypes = emptyFunction;
ReactPropTypes.checkPropTypesWithErrors = emptyFunction;
ReactPropTypes.PropTypes = ReactPropTypes;

return ReactPropTypes;
Expand Down
2 changes: 2 additions & 0 deletions factoryWithTypeCheckers.js
Expand Up @@ -15,6 +15,7 @@ var warning = require('fbjs/lib/warning');

var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
var checkPropTypes = require('./checkPropTypes');
var checkPropTypesWithErrors = require('./checkPropTypesWithErrors');

module.exports = function(isValidElement, throwOnDirectAccess) {
/* global Symbol */
Expand Down Expand Up @@ -506,6 +507,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
}

ReactPropTypes.checkPropTypes = checkPropTypes;
ReactPropTypes.checkPropTypesWithErrors = checkPropTypesWithErrors;
ReactPropTypes.PropTypes = ReactPropTypes;

return ReactPropTypes;
Expand Down

0 comments on commit 92c745e

Please sign in to comment.