diff --git a/Libraries/JavaScriptAppEngine/Initialization/__tests__/parseErrorStack-test.js b/Libraries/JavaScriptAppEngine/Initialization/__tests__/parseErrorStack-test.js new file mode 100644 index 000000000..b32c5acfa --- /dev/null +++ b/Libraries/JavaScriptAppEngine/Initialization/__tests__/parseErrorStack-test.js @@ -0,0 +1,49 @@ +/** + * 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. + */ +'use strict'; + + +require('mock-modules').autoMockOff(); + +var parseErrorStack = require('parseErrorStack'); + +function getFakeError() { + return new Error('Happy Cat'); +} + +describe('parseErrorStack', function() { + + it('parses error stack', function() { + var stack = parseErrorStack(getFakeError()); + expect(stack.length).toBeGreaterThan(0); + + var firstFrame = stack[0]; + expect(firstFrame.methodName).toEqual('getFakeError'); + expect(firstFrame.file).toMatch(/parseErrorStack-test\.js$/); + }); + + it('supports framesToPop', function() { + function getWrappedError() { + var error = getFakeError(); + error.framesToPop = 1; + return error; + } + + // Make sure framesToPop == 1 causes it to ignore getFakeError + // stack frame + var stack = parseErrorStack(getWrappedError()); + expect(stack[0].methodName).toEqual('getWrappedError'); + }); + + it('ignores bad inputs', function() { + expect(parseErrorStack({})).toEqual([]); + expect(parseErrorStack(null)).toEqual([]); + }); + +}); diff --git a/Libraries/JavaScriptAppEngine/Initialization/parseErrorStack.js b/Libraries/JavaScriptAppEngine/Initialization/parseErrorStack.js index 9dd7c47bb..bbaa1a276 100644 --- a/Libraries/JavaScriptAppEngine/Initialization/parseErrorStack.js +++ b/Libraries/JavaScriptAppEngine/Initialization/parseErrorStack.js @@ -28,6 +28,10 @@ function resolveSourceMaps(sourceMapInstance, stackFrame) { } function parseErrorStack(e, sourceMapInstance) { + if (!e || !e.stack) { + return []; + } + var stack = stacktraceParser.parse(e.stack); var framesToPop = e.framesToPop || 0;