Skip to content

Commit

Permalink
checkPropTypes: add argument that allows external logging
Browse files Browse the repository at this point in the history
When specified, the argument `warningLogger` will be called with the
same arguments as `warning`. Instead of logging errors to the fbjs
warning logger, we are able to handle them externally.

Fixes facebook#34
  • Loading branch information
rufman committed Oct 4, 2017
1 parent 155f4cc commit 0263e43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 21 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ describe('PropTypesDevelopmentStandalone', () => {
);
expect(returnValue).toBe(undefined);
});

it('calls the passed in warning logger', () => {
const warningLogger = jest.fn()
const propTypes = {
foo(props, propName, componentName) {
throw new Error('some error');
},
};
const props = {foo: 'foo'};
const returnValue = PropTypes.checkPropTypes(
propTypes,
props,
'prop',
'testComponent',
null,
warningLogger,
);

expect(warningLogger).toBeCalledWith(false, 'Failed %s type: %s%s', 'prop', 'some error', '');
expect(returnValue).toBe(undefined);
});
});

describe('Primitive Types', () => {
Expand Down
8 changes: 6 additions & 2 deletions checkPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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, warningLogger = null) {
if (process.env.NODE_ENV !== 'production') {
for (var typeSpecName in typeSpecs) {
if (typeSpecs.hasOwnProperty(typeSpecName)) {
Expand All @@ -49,7 +49,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 (warningLogger) {
warningLogger(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
} else {
warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
}
}
}
}
Expand Down

0 comments on commit 0263e43

Please sign in to comment.