Skip to content

Commit

Permalink
Merge pull request #39 from MindscapeHQ/md/sup/disable-promise-reject…
Browse files Browse the repository at this point in the history
…ion-handling

Allow for the disabling of unhandled promise rejection reporting
  • Loading branch information
mduncan26 authored Mar 3, 2021
2 parents e61e02d + 69475be commit ad38944
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const options: RaygunClientOptions = {
version: "0.1.2",
enableCrashReporting: true,
disableNativeCrashReporting: false,
disableUnhandledPromiseRejectionReporting: false,
enableRealUserMonitoring: true,
disableNetworkMonitoring: false,
customCrashReportingEndpoint: "https://myCrashReportingEndpoint.com",
Expand Down Expand Up @@ -668,6 +669,7 @@ export type RaygunClientOptions = {
version?: string;
enableCrashReporting?: boolean;
disableNativeCrashReporting?: boolean;
disableUnhandledPromiseRejectionReporting?: boolean;
enableRealUserMonitoring?: boolean;
disableNetworkMonitoring?: boolean;
customCrashReportingEndpoint?: string;
Expand Down
1 change: 1 addition & 0 deletions demo/screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const options: RaygunClientOptions = {
logLevel: LogLevel.verbose,
// Other settings to customize your Raygun usage
// disableNativeCrashReporting: true,
// disableUnhandledPromiseRejectionReporting: true,
// disableNetworkMonitoring: true,
// customCrashReportingEndpoint: "http://some-url-of-your-choice",
// customRealUserMonitoringEndpoint: "http://some-url-of-your-choice"
Expand Down
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "raygun4reactnative",
"title": "Raygun4reactnative",
"version": "1.0.0",
"version": "1.0.1",
"description": "Raygun React Native SDK",
"main": "dist/index.js",
"typescript": {
Expand Down
34 changes: 21 additions & 13 deletions sdk/src/CrashReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import RaygunLogger from "./RaygunLogger";
const {RaygunNativeBridge} = NativeModules;
const {version: clientVersion} = require('../package.json');

const {polyfillGlobal} = require('react-native/Libraries/Utilities/PolyfillFunctions')
const Promise = require('promise/setimmediate/es6-extensions')
const tracking = require('promise/setimmediate/rejection-tracking')

/**
* The Crash Reporter is responsible for all of the functionality related to generating, catching
* formatting, caching and transmitting Crash Reports as well as managing users custom data
Expand All @@ -38,13 +34,15 @@ export default class CrashReporter {
*
* @param apiKey - Access key for Raygun API
* @param disableNativeCrashReporting - Whether or not to enable Native side error reporting
* @param disableUnhandledPromiseRejectionReporting - Whether or not to enable unhandled promise rejection reporting
* @param customCrashReportingEndpoint - Custom endpoint for Crash Report (may be empty or null)
* @param onBeforeSendingCrashReport - A lambda to execute before each Crash Report transmission
* @param version - The current version of the RaygunClient
*/
constructor(
apiKey: string,
disableNativeCrashReporting: boolean,
disableUnhandledPromiseRejectionReporting: boolean,
customCrashReportingEndpoint: string,
onBeforeSendingCrashReport: BeforeSendHandler | null,
version: string
Expand All @@ -59,22 +57,28 @@ export default class CrashReporter {
this.raygunCrashReportEndpoint = customCrashReportingEndpoint;
}

//Set up error handler to divert errors to crash reporter
// Set up error handler to divert errors to crash reporter
const prevHandler = ErrorUtils.getGlobalHandler();
ErrorUtils.setGlobalHandler(async (error: Error, isFatal?: boolean) => {
await this.processUnhandledError(error, isFatal);
prevHandler && prevHandler(error, isFatal);
});

//Set up rejection handler to divert rejections to crash reporter
polyfillGlobal('Promise', () => {
tracking.enable({
allRejections: true,
onUnhandled: this.processUnhandledRejection.bind(this),
})
if (!disableUnhandledPromiseRejectionReporting) {
const {polyfillGlobal} = require('react-native/Libraries/Utilities/PolyfillFunctions');
const Promise = require('promise/setimmediate/es6-extensions');
const tracking = require('promise/setimmediate/rejection-tracking');

return Promise
})
// Set up rejection handler to divert rejections to crash reporter
polyfillGlobal('Promise', () => {
tracking.enable({
allRejections: true,
onUnhandled: this.processUnhandledRejection.bind(this),
});

return Promise;
});
}

this.resendCachedReports().then(r => {});
}
Expand Down Expand Up @@ -316,6 +320,10 @@ export default class CrashReporter {
* @param error - The caught rejection
*/
processUnhandledRejection(id: string, error: Error) {
if (__DEV__) {
console.warn(id, error);
}

this.processUnhandledError(error, false);
}

Expand Down
2 changes: 2 additions & 0 deletions sdk/src/RaygunClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const init = (raygunClientOptions: RaygunClientOptions) => {
version = '',
enableCrashReporting = false,
disableNativeCrashReporting = false,
disableUnhandledPromiseRejectionReporting = false,
enableRealUserMonitoring = false,
disableNetworkMonitoring = false,
customCrashReportingEndpoint = '',
Expand All @@ -76,6 +77,7 @@ const init = (raygunClientOptions: RaygunClientOptions) => {
crashReporter = new CrashReporter(
apiKey,
disableNativeCrashReporting,
disableUnhandledPromiseRejectionReporting,
customCrashReportingEndpoint || '',
onBeforeSendingCrashReport as BeforeSendHandler,
version
Expand Down
1 change: 1 addition & 0 deletions sdk/src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type RaygunClientOptions = {
version?: string;
enableCrashReporting?: boolean;
disableNativeCrashReporting?: boolean;
disableUnhandledPromiseRejectionReporting?: boolean;
enableRealUserMonitoring?: boolean;
disableNetworkMonitoring?: boolean;
customCrashReportingEndpoint?: string;
Expand Down

0 comments on commit ad38944

Please sign in to comment.