Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit 2b39bb1

Browse files
committed
checkPropTypes: add argument that allows external logging
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 #34
1 parent f2cf875 commit 2b39bb1

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

__tests__/PropTypesDevelopmentStandalone-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ describe('PropTypesDevelopmentStandalone', () => {
146146
expect(console.error.calls.argsFor(0)[0]).toContain('some error');
147147
expect(returnValue).toBe(undefined);
148148
});
149+
150+
it('calls the passed in warning logger', () => {
151+
const warningLogger = jest.fn()
152+
const propTypes = {
153+
foo(props, propName, componentName) {
154+
throw new Error('some error');
155+
},
156+
};
157+
const props = {foo: 'foo'};
158+
const returnValue = PropTypes.checkPropTypes(
159+
propTypes,
160+
props,
161+
'prop',
162+
'testComponent',
163+
null,
164+
warningLogger,
165+
);
166+
167+
expect(warningLogger).toBeCalledWith(false, 'Failed %s type: %s%s', 'prop', 'some error', '');
168+
expect(returnValue).toBe(undefined);
169+
});
149170
});
150171

151172
describe('Primitive Types', () => {

checkPropTypes.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if (process.env.NODE_ENV !== 'production') {
2727
* @param {?Function} getStack Returns the component stack.
2828
* @private
2929
*/
30-
function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
30+
function checkPropTypes(typeSpecs, values, location, componentName, getStack, warningLogger = null) {
3131
if (process.env.NODE_ENV !== 'production') {
3232
for (var typeSpecName in typeSpecs) {
3333
if (typeSpecs.hasOwnProperty(typeSpecName)) {
@@ -51,7 +51,11 @@ function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
5151

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

54-
warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
54+
if (warningLogger) {
55+
warningLogger(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
56+
} else {
57+
warning(false, 'Failed %s type: %s%s', location, error.message, stack != null ? stack : '');
58+
}
5559
}
5660
}
5761
}

0 commit comments

Comments
 (0)