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

Commit 0263e43

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 155f4cc commit 0263e43

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

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

checkPropTypes.js

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

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

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

0 commit comments

Comments
 (0)