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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

- [browser] fix: Make `addBreadcrumb` sync internally, `beforeBreadcrumb` is now only sync
- [browser] fix: Remove internal `console` guard in `beforeBreadcrumb`
- [core]: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client
- [core] feat: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client
itself can decide which integration should run.
- [node] ref: Simplify Node global handlers code

## 4.1.1

Expand Down
21 changes: 8 additions & 13 deletions packages/node/src/integrations/onuncaughtexception.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCurrentHub, Scope } from '@sentry/core';
import { Integration, SentryEvent, Severity } from '@sentry/types';
import { Integration, Severity } from '@sentry/types';
import { logger } from '@sentry/utils/logger';
import { defaultOnFatalError } from '../handlers';

Expand Down Expand Up @@ -33,7 +33,7 @@ export class OnUncaughtException implements Integration {
* @inheritDoc
*/
public setupOnce(): void {
global.process.on('uncaughtException', this.handler);
global.process.on('uncaughtException', this.handler.bind(this));
}
}

Expand All @@ -49,23 +49,18 @@ export function makeErrorHandler(

return (error: Error): void => {
if (!caughtFirstError) {
const hub = getCurrentHub();

// this is the first uncaught error and the ultimate reason for shutting down
// we want to do absolutely everything possible to ensure it gets captured
// also we want to make sure we don't go recursion crazy if more errors happen after this one
firstError = error;
caughtFirstError = true;

if (getCurrentHub().getIntegration(OnUncaughtException)) {
getCurrentHub().withScope(async () => {
getCurrentHub().configureScope((scope: Scope) => {
scope.addEventProcessor(async (event: SentryEvent) => ({
...event,
level: Severity.Fatal,
}));
});

getCurrentHub().captureException(error, { originalException: error });

if (hub.getIntegration(OnUncaughtException)) {
hub.withScope(async (scope: Scope) => {
scope.setLevel(Severity.Fatal);
hub.captureException(error, { originalException: error });
if (!calledFatalError) {
calledFatalError = true;
onFatalError(error);
Expand Down
46 changes: 25 additions & 21 deletions packages/node/src/integrations/onunhandledrejection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { getCurrentHub, Scope } from '@sentry/core';
import { Integration } from '@sentry/types';

/** Global Promise Rejection handler */
Expand All @@ -25,29 +25,33 @@ export class OnUnhandledRejection implements Integration {
* @param promise promise
*/
public sendUnhandledPromise(reason: any, promise: any): void {
if (!getCurrentHub().getIntegration(OnUnhandledRejection)) {
const hub = getCurrentHub();

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

const context = (promise.domain && promise.domain.sentryContext) || {};
getCurrentHub().withScope(() => {
getCurrentHub().configureScope(scope => {
// Preserve backwards compatibility with raven-node for now
if (context.user) {
scope.setUser(context.user);
}
if (context.tags) {
Object.keys(context.tags).forEach(key => {
scope.setTag(key, context.tags[key]);
});
}
if (context.extra) {
Object.keys(context.extra).forEach(key => {
scope.setExtra(key, context.extra[key]);
});
}
scope.setExtra('unhandledPromiseRejection', true);
});
getCurrentHub().captureException(reason, { originalException: promise });

hub.withScope((scope: Scope) => {
scope.setExtra('unhandledPromiseRejection', true);

// Preserve backwards compatibility with raven-node for now
if (context.user) {
scope.setUser(context.user);
}
if (context.tags) {
Object.keys(context.tags).forEach(key => {
scope.setTag(key, context.tags[key]);
});
}
if (context.extra) {
Object.keys(context.extra).forEach(key => {
scope.setExtra(key, context.extra[key]);
});
}

hub.captureException(reason, { originalException: promise });
});
}
}
3 changes: 2 additions & 1 deletion packages/node/test/onunhandledrejection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('unhandled promises', () => {

expect(captureException.mock.calls[0][0]).toBe('bla');
expect(setUser.mock.calls[0][0]).toEqual({ id: 1 });
expect(setExtra.mock.calls[0]).toEqual(['extra', '1']);
expect(setExtra.mock.calls[0]).toEqual(['unhandledPromiseRejection', true]);
expect(setExtra.mock.calls[1]).toEqual(['extra', '1']);
expect(setTag.mock.calls[0]).toEqual(['tag', '2']);
});
});