throw-on-react-error.js 958 B

123456789101112131415161718192021222324252627282930
  1. // eslint-disable-next-line no-console
  2. const originalConsoleError = console.error;
  3. // List of `console.error` messages to fail on
  4. const BAD_ERRORS = [
  5. 'Failed prop type',
  6. 'Failed child context type',
  7. 'Warning: Each child in an array or iterator should have a unique "key" prop',
  8. 'React does not recognize the `[^`]+` prop on a DOM element',
  9. ];
  10. // This is needed because when we throw the captured error message, it will
  11. // also `console.error` it
  12. const REPEATED_ERROR = 'Error: Uncaught [Error: Warning: ';
  13. const BAD_ERRORS_REGEX = new RegExp(BAD_ERRORS.join('|'));
  14. // eslint-disable-next-line no-console
  15. console.error = (message, ...args) => {
  16. if (
  17. typeof message === 'string' &&
  18. message.indexOf(REPEATED_ERROR) !== 0 &&
  19. BAD_ERRORS_REGEX.test(message)
  20. ) {
  21. originalConsoleError(message, ...args);
  22. throw new Error(message);
  23. } else if (!BAD_ERRORS_REGEX.test(message)) {
  24. originalConsoleError(message, ...args);
  25. }
  26. };