2015-03-19 16:51:33 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* 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.
|
2015-03-19 16:51:33 +00:00
|
|
|
*
|
|
|
|
* @providesModule ExceptionsManager
|
2015-03-26 17:06:50 +00:00
|
|
|
* @flow
|
2015-03-19 16:51:33 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Platform = require('Platform');
|
|
|
|
var RCTExceptionsManager = require('NativeModules').ExceptionsManager;
|
|
|
|
|
|
|
|
var loadSourceMap = require('loadSourceMap');
|
|
|
|
var parseErrorStack = require('parseErrorStack');
|
|
|
|
|
|
|
|
var sourceMapPromise;
|
|
|
|
|
2015-03-26 17:06:50 +00:00
|
|
|
type Exception = {
|
|
|
|
sourceURL: string;
|
|
|
|
line: number;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
2015-04-23 18:34:25 +00:00
|
|
|
function handleException(e: Exception) {
|
|
|
|
var stack = parseErrorStack(e);
|
|
|
|
console.error(
|
|
|
|
'Err0r: ' +
|
|
|
|
'\n stack: \n' + stackToString(stack) +
|
|
|
|
'\n URL: ' + e.sourceURL +
|
|
|
|
'\n line: ' + e.line +
|
|
|
|
'\n message: ' + e.message
|
|
|
|
);
|
|
|
|
|
2015-03-19 16:51:33 +00:00
|
|
|
if (RCTExceptionsManager) {
|
2015-04-02 22:01:06 +00:00
|
|
|
RCTExceptionsManager.reportUnhandledException(e.message, stack);
|
2015-03-19 16:51:33 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
(sourceMapPromise = sourceMapPromise || loadSourceMap())
|
|
|
|
.then(map => {
|
|
|
|
var prettyStack = parseErrorStack(e, map);
|
2015-04-02 22:01:06 +00:00
|
|
|
RCTExceptionsManager.updateExceptionMessage(e.message, prettyStack);
|
2015-03-19 16:51:33 +00:00
|
|
|
})
|
|
|
|
.then(null, error => {
|
|
|
|
console.error('#CLOWNTOWN (error while displaying error): ' + error.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stackToString(stack) {
|
|
|
|
var maxLength = Math.max.apply(null, stack.map(frame => frame.methodName.length));
|
|
|
|
return stack.map(frame => stackFrameToString(frame, maxLength)).join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
function stackFrameToString(stackFrame, maxLength) {
|
|
|
|
var fileNameParts = stackFrame.file.split('/');
|
|
|
|
var fileName = fileNameParts[fileNameParts.length - 1];
|
|
|
|
|
|
|
|
if (fileName.length > 18) {
|
|
|
|
fileName = fileName.substr(0, 17) + '\u2026' /* ... */;
|
|
|
|
}
|
|
|
|
|
|
|
|
var spaces = fillSpaces(maxLength - stackFrame.methodName.length);
|
|
|
|
return ' ' + stackFrame.methodName + spaces + ' ' + fileName + ':' + stackFrame.lineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fillSpaces(n) {
|
|
|
|
return new Array(n + 1).join(' ');
|
|
|
|
}
|
|
|
|
|
2015-04-23 18:34:25 +00:00
|
|
|
module.exports = { handleException };
|