2015-03-19 16:51:33 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-19 16:51:33 +00:00
|
|
|
*
|
2015-03-26 17:06:50 +00:00
|
|
|
* @flow
|
2015-03-19 16:51:33 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
import type {ExtendedError} from 'parseErrorStack';
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
/**
|
|
|
|
* Handles the developer-visible aspect of errors and exceptions
|
|
|
|
*/
|
2016-10-11 13:51:46 +00:00
|
|
|
let exceptionID = 0;
|
2017-06-14 16:16:26 +00:00
|
|
|
function reportException(e: ExtendedError, isFatal: bool) {
|
2016-10-11 13:51:46 +00:00
|
|
|
const {ExceptionsManager} = require('NativeModules');
|
|
|
|
if (ExceptionsManager) {
|
|
|
|
const parseErrorStack = require('parseErrorStack');
|
2016-01-27 22:55:02 +00:00
|
|
|
const stack = parseErrorStack(e);
|
2016-10-11 13:51:46 +00:00
|
|
|
const currentExceptionID = ++exceptionID;
|
2015-06-02 14:28:56 +00:00
|
|
|
if (isFatal) {
|
2016-10-11 13:51:46 +00:00
|
|
|
ExceptionsManager.reportFatalException(e.message, stack, currentExceptionID);
|
2015-05-13 17:56:09 +00:00
|
|
|
} else {
|
2016-10-11 13:51:46 +00:00
|
|
|
ExceptionsManager.reportSoftException(e.message, stack, currentExceptionID);
|
2015-05-13 17:56:09 +00:00
|
|
|
}
|
2015-03-19 16:51:33 +00:00
|
|
|
if (__DEV__) {
|
2016-10-11 13:51:46 +00:00
|
|
|
const symbolicateStackTrace = require('symbolicateStackTrace');
|
2016-06-01 20:50:34 +00:00
|
|
|
symbolicateStackTrace(stack).then(
|
2016-06-28 16:33:52 +00:00
|
|
|
(prettyStack) => {
|
|
|
|
if (prettyStack) {
|
2016-10-11 13:51:46 +00:00
|
|
|
ExceptionsManager.updateExceptionMessage(e.message, prettyStack, currentExceptionID);
|
2016-06-28 16:33:52 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('The stack is null');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).catch(
|
2016-10-11 13:51:46 +00:00
|
|
|
(error) => console.warn('Unable to symbolicate stack trace: ' + error.message)
|
2016-06-01 20:50:34 +00:00
|
|
|
);
|
2015-03-19 16:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:46 +00:00
|
|
|
declare var console: typeof console & {
|
|
|
|
_errorOriginal: Function,
|
|
|
|
reportErrorsAsExceptions: boolean,
|
|
|
|
};
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
/**
|
|
|
|
* Logs exceptions to the (native) console and displays them
|
|
|
|
*/
|
2015-09-24 00:11:35 +00:00
|
|
|
function handleException(e: Error, isFatal: boolean) {
|
|
|
|
// Workaround for reporting errors caused by `throw 'some string'`
|
|
|
|
// Unfortunately there is no way to figure out the stacktrace in this
|
|
|
|
// case, so if you ended up here trying to trace an error, look for
|
|
|
|
// `throw '<error message>'` somewhere in your codebase.
|
|
|
|
if (!e.message) {
|
|
|
|
e = new Error(e);
|
|
|
|
}
|
2016-10-11 13:51:46 +00:00
|
|
|
if (console._errorOriginal) {
|
2016-09-06 20:36:33 +00:00
|
|
|
console._errorOriginal(e.message);
|
|
|
|
} else {
|
|
|
|
console.error(e.message);
|
|
|
|
}
|
2015-11-05 20:20:15 +00:00
|
|
|
reportException(e, isFatal);
|
2015-04-28 22:41:33 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:46 +00:00
|
|
|
function reactConsoleErrorHandler() {
|
|
|
|
console._errorOriginal.apply(console, arguments);
|
|
|
|
if (!console.reportErrorsAsExceptions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments[0] && arguments[0].stack) {
|
|
|
|
reportException(arguments[0], /* isFatal */ false);
|
|
|
|
} else {
|
|
|
|
const stringifySafe = require('stringifySafe');
|
|
|
|
const str = Array.prototype.map.call(arguments, stringifySafe).join(', ');
|
|
|
|
if (str.slice(0, 10) === '"Warning: ') {
|
|
|
|
// React warnings use console.error so that a stack trace is shown, but
|
|
|
|
// we don't (currently) want these to show a redbox
|
|
|
|
// (Note: Logic duplicated in polyfills/console.js.)
|
|
|
|
return;
|
|
|
|
}
|
2017-06-14 16:16:26 +00:00
|
|
|
const error : ExtendedError = new Error('console.error: ' + str);
|
2016-10-11 13:51:46 +00:00
|
|
|
error.framesToPop = 1;
|
|
|
|
reportException(error, /* isFatal */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:41:33 +00:00
|
|
|
/**
|
|
|
|
* Shows a redbox with stacktrace for all console.error messages. Disable by
|
|
|
|
* setting `console.reportErrorsAsExceptions = false;` in your app.
|
|
|
|
*/
|
|
|
|
function installConsoleErrorReporter() {
|
2015-11-05 20:20:15 +00:00
|
|
|
// Enable reportErrorsAsExceptions
|
|
|
|
if (console._errorOriginal) {
|
2015-04-28 22:41:33 +00:00
|
|
|
return; // already installed
|
|
|
|
}
|
2016-08-03 03:46:16 +00:00
|
|
|
// Flow doesn't like it when you set arbitrary values on a global object
|
2016-10-11 13:51:46 +00:00
|
|
|
console._errorOriginal = console.error.bind(console);
|
|
|
|
console.error = reactConsoleErrorHandler;
|
2015-04-28 22:41:33 +00:00
|
|
|
if (console.reportErrorsAsExceptions === undefined) {
|
2016-10-11 13:51:46 +00:00
|
|
|
// Individual apps can disable this
|
2016-08-03 03:46:16 +00:00
|
|
|
// Flow doesn't like it when you set arbitrary values on a global object
|
2016-10-11 13:51:46 +00:00
|
|
|
console.reportErrorsAsExceptions = true;
|
2015-04-28 22:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { handleException, installConsoleErrorReporter };
|