Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rnerrorhanlers): Don't crash if ErrorUtils are not available #2808

Merged
merged 10 commits into from
Feb 9, 2023
105 changes: 54 additions & 51 deletions src/js/integrations/reactnativeerrorhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,69 +161,72 @@ export class ReactNativeErrorHandlers implements Integration {
if (this._options.onerror) {
let handlingFatal = false;

const defaultHandler =
ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
ErrorUtils.setGlobalHandler(async (error: any, isFatal?: boolean) => {
// We want to handle fatals, but only in production mode.
const shouldHandleFatal = isFatal && !__DEV__;
if (shouldHandleFatal) {
if (handlingFatal) {
logger.log(
'Encountered multiple fatals in a row. The latest:',
error
);
return;
try {
const defaultHandler =
ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
ErrorUtils.setGlobalHandler(async (error: any, isFatal?: boolean) => {
// We want to handle fatals, but only in production mode.
const shouldHandleFatal = isFatal && !__DEV__;
if (shouldHandleFatal) {
if (handlingFatal) {
logger.log(
'Encountered multiple fatals in a row. The latest:',
error
);
return;
}
handlingFatal = true;
}
handlingFatal = true;
}

const currentHub = getCurrentHub();
const client = currentHub.getClient<ReactNativeClient>();
const scope = currentHub.getScope();
const currentHub = getCurrentHub();
const client = currentHub.getClient<ReactNativeClient>();
const scope = currentHub.getScope();

if (!client) {
logger.error(
'Sentry client is missing, the error event might be lost.',
error
);
if (!client) {
logger.error(
'Sentry client is missing, the error event might be lost.',
error
);

// If there is no client something is fishy, anyway we call the default handler
defaultHandler(error, isFatal);
// If there is no client something is fishy, anyway we call the default handler
defaultHandler(error, isFatal);

return;
}
return;
}

const options = client.getOptions();
const options = client.getOptions();

const hint: EventHint = {
originalException: error,
attachments: scope?.getAttachments(),
};
const event = await client.eventFromException(error, hint);
const hint: EventHint = {
originalException: error,
attachments: scope?.getAttachments(),
};
const event = await client.eventFromException(error, hint);

if (isFatal) {
event.level = 'fatal' as SeverityLevel;
if (isFatal) {
event.level = 'fatal' as SeverityLevel;

addExceptionMechanism(event, {
handled: false,
type: 'onerror',
});
}
addExceptionMechanism(event, {
handled: false,
type: 'onerror',
});
}

currentHub.captureEvent(event, hint);
currentHub.captureEvent(event, hint);

if (!__DEV__) {
void client.flush(options.shutdownTimeout || 2000).then(() => {
if (!__DEV__) {
void client.flush(options.shutdownTimeout || 2000).then(() => {
defaultHandler(error, isFatal);
});
} else {
// If in dev, we call the default handler anyway and hope the error will be sent
// Just for a better dev experience
defaultHandler(error, isFatal);
});
} else {
// If in dev, we call the default handler anyway and hope the error will be sent
// Just for a better dev experience
defaultHandler(error, isFatal);
}
});
}
});
} catch(e) {
console.warn('react-native-web does not support ErrorUtils');
}
}
}