Skip to content

Commit f35c969

Browse files
TheBuggedYRNymabdallah
authored andcommitted
[MOB-11562] Fix Original Error Handler Not Called (#886)
The original global error handler was initialized in the global scope, so it used to have the default value set by React Native. When users try to set a global error handler themselves, the handler will be ignored, due to the early initialization of `originalHandler`. This PR fixes this issue by getting the global error handler within Instabug.init scope. Allowing the users to set global error handlers before initializing Instabug. Setting handlers after Instabug's initialization would still not be possible.
1 parent 7e4e64e commit f35c969

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
- Fixes global error handler not being called.
34
- Deprecates Instabug.start in favour of Instabug.init that takes a configuration object for SDK initialization.
45
- Deprecates Instabug.setDebugEnabled, Instabug.setSdkDebugLogsLevel, and APM.setLogLevel in favour of debugLogsLevel property, which can be passed to InstabugConfig while initializing the SDK using Instabug.init.
56
- Deprecates the enums: sdkDebugLogsLevel and logLevel in favour of a new enum LogLevel.

src/utils/InstabugUtils.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ export const getStackTrace = (e: ExtendedError): StackFrame[] => {
6767
return jsStackTrace;
6868
};
6969

70-
const originalHandler = ErrorUtils.getGlobalHandler();
71-
7270
export const captureJsErrors = () => {
7371
if (!process.env.JEST_WORKER_ID) {
7472
if (__DEV__) {
7573
return;
7674
}
7775
}
7876

79-
const errorHandler: ErrorHandlerCallback = (e, isFatal) => {
80-
const jsStackTrace = getStackTrace(e);
77+
const originalErrorHandler = ErrorUtils.getGlobalHandler();
78+
79+
const instabugErrorHandler: ErrorHandlerCallback = (err) => {
80+
const jsStackTrace = getStackTrace(err);
8181

8282
// JSON object to be sent to the native SDK
8383
const jsonObject = {
84-
message: e.name + ' - ' + e.message,
85-
e_message: e.message,
86-
e_name: e.name,
84+
message: err.name + ' - ' + err.message,
85+
e_message: err.message,
86+
e_name: err.name,
8787
os: Platform.OS,
8888
platform: 'react_native',
8989
exception: jsStackTrace,
@@ -98,19 +98,14 @@ export const captureJsErrors = () => {
9898
} else {
9999
NativeCrashReporting.sendJSCrash(jsonObject);
100100
}
101-
102-
if (originalHandler && !process.env.JEST_WORKER_ID) {
103-
if (Platform.OS === 'ios') {
104-
originalHandler(e, isFatal);
105-
} else {
106-
setTimeout(() => {
107-
originalHandler(e, isFatal);
108-
}, 500);
109-
}
110-
}
111101
};
112102

113-
ErrorUtils.setGlobalHandler(errorHandler);
103+
ErrorUtils.setGlobalHandler((err, isFatal) => {
104+
if (!process.env.JEST_WORKER_ID) {
105+
originalErrorHandler(err, isFatal);
106+
}
107+
instabugErrorHandler(err, isFatal);
108+
});
114109
};
115110

116111
export const stringifyIfNotString = (input: unknown) => {

0 commit comments

Comments
 (0)