mirror of
https://github.com/status-im/react-native.git
synced 2025-01-17 21:11:45 +00:00
946ec48a07
Summary: See "D3291793 Symbolicate JS stacktrace using RN Packager" for the background of why this matters. TLDR - saving tons of memory by moving symbolication from JSC into server and cleaning up old hacks. Before this change: a redbox causes +73MB JSC size {F60869250} {F60869249} After this change: a redbox causes +1MB JSC size {F61005061} {F61005062} Next step – replace JS implementation by native to show better progress and clean up even more old APIs (ExceptionsManager.updateExceptionMessage). Reviewed By: davidaurelio Differential Revision: D3319151 fbshipit-source-id: 48ff4df27642ea4e1bc2414f48a8dd4d32adee50
108 lines
3.7 KiB
JavaScript
108 lines
3.7 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule ExceptionsManager
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
let exceptionID = 0;
|
|
|
|
/**
|
|
* Handles the developer-visible aspect of errors and exceptions
|
|
*/
|
|
function reportException(e: Error, isFatal: bool) {
|
|
const parseErrorStack = require('parseErrorStack');
|
|
const RCTExceptionsManager = require('NativeModules').ExceptionsManager;
|
|
|
|
const currentExceptionID = ++exceptionID;
|
|
if (RCTExceptionsManager) {
|
|
const stack = parseErrorStack(e);
|
|
if (isFatal) {
|
|
RCTExceptionsManager.reportFatalException(e.message, stack, currentExceptionID);
|
|
} else {
|
|
RCTExceptionsManager.reportSoftException(e.message, stack, currentExceptionID);
|
|
}
|
|
if (__DEV__) {
|
|
symbolicateAndUpdateStack(currentExceptionID, e.message, stack);
|
|
}
|
|
}
|
|
}
|
|
|
|
function symbolicateAndUpdateStack(id, message, stack) {
|
|
const {fetch} = require('fetch');
|
|
const {SourceCode, ExceptionsManager} = require('NativeModules');
|
|
const match = SourceCode.scriptURL && SourceCode.scriptURL.match(/^https?:\/\/.*?\//);
|
|
const endpoint = (match && match[0] : 'http://localhost:8081/') + 'symbolicate';
|
|
|
|
fetch(endpoint, { method: 'POST', body: JSON.stringify({stack}) })
|
|
.then(response => response.json())
|
|
.then(response =>
|
|
ExceptionsManager.updateExceptionMessage(message, response.stack, id))
|
|
.catch(error => {
|
|
// This can happen in a variety of normal situations, such as
|
|
// Network module not being available, or when running locally
|
|
console.warn('Unable to symbolicate stack trace: ' + error.message);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Logs exceptions to the (native) console and displays them
|
|
*/
|
|
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);
|
|
}
|
|
|
|
(console._errorOriginal || console.error)(e.message);
|
|
reportException(e, isFatal);
|
|
}
|
|
|
|
/**
|
|
* Shows a redbox with stacktrace for all console.error messages. Disable by
|
|
* setting `console.reportErrorsAsExceptions = false;` in your app.
|
|
*/
|
|
function installConsoleErrorReporter() {
|
|
// Enable reportErrorsAsExceptions
|
|
if (console._errorOriginal) {
|
|
return; // already installed
|
|
}
|
|
console._errorOriginal = console.error.bind(console);
|
|
console.error = function reactConsoleError() {
|
|
console._errorOriginal.apply(null, 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;
|
|
}
|
|
const error : any = new Error('console.error: ' + str);
|
|
error.framesToPop = 1;
|
|
reportException(error, /* isFatal */ false);
|
|
}
|
|
};
|
|
if (console.reportErrorsAsExceptions === undefined) {
|
|
console.reportErrorsAsExceptions = true; // Individual apps can disable this
|
|
}
|
|
}
|
|
|
|
module.exports = { handleException, installConsoleErrorReporter };
|