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

Commit 5351558

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 8abb8d7 commit 5351558

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
@@ -177,6 +177,27 @@ describe('PropTypesDevelopmentStandalone', () => {
177177
);
178178
expect(returnValue).toBe(undefined);
179179
});
180+
181+
it('calls the passed in warning logger', () => {
182+
const warningLogger = jest.fn()
183+
const propTypes = {
184+
foo(props, propName, componentName) {
185+
throw new Error('some error');
186+
},
187+
};
188+
const props = {foo: 'foo'};
189+
const returnValue = PropTypes.checkPropTypes(
190+
propTypes,
191+
props,
192+
'prop',
193+
'testComponent',
194+
null,
195+
warningLogger,
196+
);
197+
198+
expect(warningLogger).toBeCalledWith(false, 'Failed %s type: %s%s', 'prop', 'some error', '');
199+
expect(returnValue).toBe(undefined);
200+
});
180201
});
181202

182203
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)