Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

- Coming soon...

## 5.9.0

- [node] feat: Added `mode` option for `OnUnhandledRejection` integration that changes how we log errors and what we do with the process itself
- [browser] ref: Both global handlers now always return `true` to call default implementations (error logging)

## 5.8.0

- [browser/node] feat: 429 http code handling in node/browser transports (#2300)
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class GlobalHandlers implements Integration {
if (self._oldOnErrorHandler) {
return self._oldOnErrorHandler.apply(this, arguments);
}
return false;
return true;
}

const client = currentHub.getClient();
Expand Down Expand Up @@ -121,7 +121,7 @@ export class GlobalHandlers implements Integration {
return self._oldOnErrorHandler.apply(this, arguments);
}

return false;
return true;
};

this._onErrorHandlerInstalled = true;
Expand Down Expand Up @@ -152,7 +152,7 @@ export class GlobalHandlers implements Integration {
if (self._oldOnUnhandledRejectionHandler) {
return self._oldOnUnhandledRejectionHandler.apply(this, arguments);
}
return false;
return true;
}

const client = currentHub.getClient();
Expand All @@ -178,7 +178,7 @@ export class GlobalHandlers implements Integration {
return self._oldOnUnhandledRejectionHandler.apply(this, arguments);
}

return false;
return true;
};

this._onUnhandledRejectionHandlerInstalled = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ export function errorHandler(options?: {
/**
* @hidden
*/
export function defaultOnFatalError(error: Error): void {
export function logAndExitProcess(error: Error): void {
console.error(error && error.stack ? error.stack : error);

const client = getCurrentHub().getClient<NodeClient>();

if (client === undefined) {
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/integrations/onuncaughtexception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Integration, Severity } from '@sentry/types';
import { logger } from '@sentry/utils';

import { NodeClient } from '../client';
import { defaultOnFatalError } from '../handlers';
import { logAndExitProcess } from '../handlers';

/** Global Promise Rejection handler */
export class OnUncaughtException implements Integration {
Expand Down Expand Up @@ -54,7 +54,7 @@ export class OnUncaughtException implements Integration {
return (error: Error): void => {
type onFatalErrorHandlerType = (firstError: Error, secondError?: Error) => void;

let onFatalError: onFatalErrorHandlerType = defaultOnFatalError;
let onFatalError: onFatalErrorHandlerType = logAndExitProcess;
const client = getCurrentHub().getClient<NodeClient>();

if (this._options.onFatalError) {
Expand Down Expand Up @@ -90,7 +90,7 @@ export class OnUncaughtException implements Integration {
} else if (calledFatalError) {
// we hit an error *after* calling onFatalError - pretty boned at this point, just shut it down
logger.warn('uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown');
defaultOnFatalError(error);
logAndExitProcess(error);
} else if (!caughtSecondError) {
// two cases for how we can hit this branch:
// - capturing of first error blew up and we just caught the exception from that
Expand Down
45 changes: 45 additions & 0 deletions packages/node/src/integrations/onunhandledrejection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getCurrentHub, Scope } from '@sentry/core';
import { Integration } from '@sentry/types';
import { consoleSandbox } from '@sentry/utils';

import { logAndExitProcess } from '../handlers';

type UnhandledRejectionMode = 'none' | 'warn' | 'strict';

/** Global Promise Rejection handler */
export class OnUnhandledRejection implements Integration {
Expand All @@ -12,6 +17,19 @@ export class OnUnhandledRejection implements Integration {
*/
public static id: string = 'OnUnhandledRejection';

/**
* @inheritDoc
*/
public constructor(
private readonly _options: {
/**
* Option deciding what to do after capturing unhandledRejection,
* that mimicks behavior of node's --unhandled-rejection flag.
*/
mode: UnhandledRejectionMode;
} = { mode: 'warn' },
) {}

/**
* @inheritDoc
*/
Expand All @@ -28,6 +46,7 @@ export class OnUnhandledRejection implements Integration {
const hub = getCurrentHub();

if (!hub.getIntegration(OnUnhandledRejection)) {
this._handleRejection(reason);
return;
}

Expand All @@ -49,5 +68,31 @@ export class OnUnhandledRejection implements Integration {

hub.captureException(reason, { originalException: promise });
});

this._handleRejection(reason);
}

/**
* Handler for `mode` option
*/
private _handleRejection(reason: any): void {
// https://github.com/nodejs/node/blob/7cf6f9e964aa00772965391c23acda6d71972a9a/lib/internal/process/promises.js#L234-L240
const rejectionWarning =
'This error originated either by ' +
'throwing inside of an async function without a catch block, ' +
'or by rejecting a promise which was not handled with .catch().' +
' The promise rejected with the reason:';

if (this._options.mode === 'warn') {
consoleSandbox(() => {
console.warn(rejectionWarning);
console.error(reason && reason.stack ? reason.stack : reason);
});
} else if (this._options.mode === 'strict') {
consoleSandbox(() => {
console.warn(rejectionWarning);
});
logAndExitProcess(reason);
}
}
}