Skip to content

Commit

Permalink
Add spec for ExceptionsManager (#24900)
Browse files Browse the repository at this point in the history
Summary:
Part of #24875, adds a spec for ExceptionsManager

## Changelog

[General] [Added] - TM Add spec for ExceptionsManager
Pull Request resolved: #24900

Reviewed By: fkgozali

Differential Revision: D15434006

Pulled By: RSNara

fbshipit-source-id: 1a505744a84c0c4ac3a9fac6c91a391fbd8a9f46
  • Loading branch information
thymikee authored and facebook-github-bot committed May 22, 2019
1 parent 65b10b3 commit 8ea749a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 23 deletions.
7 changes: 1 addition & 6 deletions Libraries/Core/Devtools/parseErrorStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@

'use strict';

export type StackFrame = {
column: ?number,
file: string,
lineNumber: number,
methodName: string,
};
import type {StackFrame} from '../NativeExceptionsManager';

export type ExtendedError = Error & {
framesToPop?: number,
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Core/Devtools/symbolicateStackTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import NativeSourceCode from '../../NativeModules/specs/NativeSourceCode';
// Avoid requiring fetch on load of this module; see symbolicateStackTrace
let fetch;

import type {StackFrame} from './parseErrorStack';
import type {StackFrame} from '../NativeExceptionsManager';

function isSourcedFromDisk(sourcePath: string): boolean {
return !/^http/.test(sourcePath) && /[\\/]/.test(sourcePath);
Expand Down
19 changes: 12 additions & 7 deletions Libraries/Core/ExceptionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @flow strict-local
*/

'use strict';
Expand All @@ -24,21 +24,25 @@ const INTERNAL_CALLSITES_REGEX = new RegExp(
*/
let exceptionID = 0;
function reportException(e: ExtendedError, isFatal: boolean) {
const {ExceptionsManager} = require('../BatchedBridge/NativeModules');
if (ExceptionsManager) {
const NativeExceptionsManager = require('./NativeExceptionsManager').default;
if (NativeExceptionsManager) {
const parseErrorStack = require('./Devtools/parseErrorStack');
const stack = parseErrorStack(e);
const currentExceptionID = ++exceptionID;
const message =
e.jsEngine == null ? e.message : `${e.message}, js engine: ${e.jsEngine}`;
if (isFatal) {
ExceptionsManager.reportFatalException(
NativeExceptionsManager.reportFatalException(
message,
stack,
currentExceptionID,
);
} else {
ExceptionsManager.reportSoftException(message, stack, currentExceptionID);
NativeExceptionsManager.reportSoftException(
message,
stack,
currentExceptionID,
);
}
if (__DEV__) {
const symbolicateStackTrace = require('./Devtools/symbolicateStackTrace');
Expand All @@ -50,7 +54,7 @@ function reportException(e: ExtendedError, isFatal: boolean) {
frame.file &&
frame.file.match(INTERNAL_CALLSITES_REGEX) === null,
);
ExceptionsManager.updateExceptionMessage(
NativeExceptionsManager.updateExceptionMessage(
message,
stackWithoutInternalCallsites,
currentExceptionID,
Expand All @@ -67,7 +71,7 @@ function reportException(e: ExtendedError, isFatal: boolean) {
}

declare var console: typeof console & {
_errorOriginal: Function,
_errorOriginal: typeof console.error,
reportErrorsAsExceptions: boolean,
};

Expand All @@ -80,6 +84,7 @@ function handleException(e: Error, isFatal: boolean) {
// case, so if you ended up here trying to trace an error, look for
// `throw '<error message>'` somewhere in your codebase.
if (!e.message) {
// $FlowFixMe - cannot reassign constant, explanation above
e = new Error(e);
}
if (console._errorOriginal) {
Expand Down
43 changes: 43 additions & 0 deletions Libraries/Core/NativeExceptionsManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export type StackFrame = {|
column: ?number,
file: string,
lineNumber: number,
methodName: string,
|};

export interface Spec extends TurboModule {
+reportFatalException: (
message: string,
stack: Array<StackFrame>,
exceptionId: number,
) => void;
+reportSoftException: (
message: string,
stack: Array<StackFrame>,
exceptionId: number,
) => void;
+updateExceptionMessage: (
message: string,
stack: Array<StackFrame>,
exceptionId: number,
) => void;
// Android only
+dismissRedbox: () => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ExceptionsManager');
10 changes: 5 additions & 5 deletions Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ Error: ${e.message}`;
) {
NativeRedBox.dismiss();
} else {
const RCTExceptionsManager = require('../BatchedBridge/NativeModules')
.ExceptionsManager;
RCTExceptionsManager &&
RCTExceptionsManager.dismissRedbox &&
RCTExceptionsManager.dismissRedbox();
const NativeExceptionsManager = require('../Core/NativeExceptionsManager')
.default;
NativeExceptionsManager &&
NativeExceptionsManager.dismissRedbox &&
NativeExceptionsManager.dismissRedbox();
}
});

Expand Down
2 changes: 1 addition & 1 deletion Libraries/YellowBox/Data/YellowBoxSymbolication.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

const symbolicateStackTrace = require('../../Core/Devtools/symbolicateStackTrace');

import type {StackFrame} from '../../Core/Devtools/parseErrorStack';
import type {StackFrame} from '../../Core/NativeExceptionsManager';

type CacheKey = string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use strict';

import type {StackFrame} from '../../../Core/Devtools/parseErrorStack';
import type {StackFrame} from '../../../Core/NativeExceptionsManager';

jest.mock('../../../Core/Devtools/symbolicateStackTrace');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'use strict';

import type {StackFrame} from '../../../Core/Devtools/parseErrorStack';
import type {StackFrame} from '../../../Core/NativeExceptionsManager';

jest.mock('../YellowBoxSymbolication');

Expand Down
2 changes: 1 addition & 1 deletion Libraries/YellowBox/UI/YellowBoxInspectorStackFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const YellowBoxPressable = require('./YellowBoxPressable');
const YellowBoxStyle = require('./YellowBoxStyle');

import type {PressEvent} from '../../Types/CoreEventTypes';
import type {StackFrame} from '../../Core/Devtools/parseErrorStack';
import type {StackFrame} from '../../Core/NativeExceptionsManager';

type Props = $ReadOnly<{|
frame: StackFrame,
Expand Down

0 comments on commit 8ea749a

Please sign in to comment.