Skip to content

Feat: Allow node stackwalk in Electron renderer #3710

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

Closed
Closed
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
86 changes: 7 additions & 79 deletions packages/node/src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { BaseBackend, getCurrentHub } from '@sentry/core';
import { Event, EventHint, Mechanism, Severity, Transport, TransportOptions } from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
Dsn,
extractExceptionKeysForMessage,
isError,
isPlainObject,
normalizeToSize,
SyncPromise,
} from '@sentry/utils';
import { BaseBackend } from '@sentry/core';
import { Event, EventHint, Severity, Transport, TransportOptions } from '@sentry/types';
import { Dsn } from '@sentry/utils';

import { extractStackFromError, parseError, parseStack, prepareFramesForEvent } from './parsers';
import { eventFromException, eventFromMessage } from './eventbuilder';
import { readFilesAddPrePostContext } from './sources';
import { HTTPSTransport, HTTPTransport } from './transports';
import { NodeOptions } from './types';

Expand All @@ -25,78 +17,14 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let ex: any = exception;
const providedMechanism: Mechanism | undefined =
hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism;
const mechanism: Mechanism = providedMechanism || {
handled: true,
type: 'generic',
};

if (!isError(exception)) {
if (isPlainObject(exception)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;

getCurrentHub().configureScope(scope => {
scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>));
});

ex = (hint && hint.syntheticException) || new Error(message);
(ex as Error).message = message;
} else {
// This handles when someone does: `throw "something awesome";`
// We use synthesized Error here so we can extract a (rough) stack trace.
ex = (hint && hint.syntheticException) || new Error(exception as string);
(ex as Error).message = exception;
}
mechanism.synthetic = true;
}

return new SyncPromise<Event>((resolve, reject) =>
parseError(ex as Error, this._options)
.then(event => {
addExceptionTypeValue(event, undefined, undefined);
addExceptionMechanism(event, mechanism);

resolve({
...event,
event_id: hint && hint.event_id,
});
})
.then(null, reject),
);
return eventFromException(this._options, exception, hint, readFilesAddPrePostContext);
}

/**
* @inheritDoc
*/
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
const event: Event = {
event_id: hint && hint.event_id,
level,
message,
};

return new SyncPromise<Event>(resolve => {
if (this._options.attachStacktrace && hint && hint.syntheticException) {
const stack = hint.syntheticException ? extractStackFromError(hint.syntheticException) : [];
void parseStack(stack, this._options)
.then(frames => {
event.stacktrace = {
frames: prepareFramesForEvent(frames),
};
resolve(event);
})
.then(null, () => {
resolve(event);
});
} else {
resolve(event);
}
});
return eventFromMessage(this._options, message, level, hint, readFilesAddPrePostContext);
}

/**
Expand Down
106 changes: 106 additions & 0 deletions packages/node/src/eventbuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { getCurrentHub } from '@sentry/core';
import { Event, EventHint, Mechanism, Severity } from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
extractExceptionKeysForMessage,
isError,
isPlainObject,
normalizeToSize,
SyncPromise,
} from '@sentry/utils';

import { extractStackFromError, parseError, parseStack, prepareFramesForEvent, ReadFilesFn } from './parsers';
import { NodeOptions } from './types';

/**
* Builds and Event from a Exception
* @hidden
*/
export function eventFromException(
options: NodeOptions,
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
exception: any,
hint?: EventHint,
readFiles?: ReadFilesFn,
): PromiseLike<Event> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let ex: any = exception;
const providedMechanism: Mechanism | undefined =
hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism;
const mechanism: Mechanism = providedMechanism || {
handled: true,
type: 'generic',
};

if (!isError(exception)) {
if (isPlainObject(exception)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;

getCurrentHub().configureScope(scope => {
scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>));
});

ex = (hint && hint.syntheticException) || new Error(message);
(ex as Error).message = message;
} else {
// This handles when someone does: `throw "something awesome";`
// We use synthesized Error here so we can extract a (rough) stack trace.
ex = (hint && hint.syntheticException) || new Error(exception as string);
(ex as Error).message = exception;
}
mechanism.synthetic = true;
}

return new SyncPromise<Event>((resolve, reject) =>
parseError(ex as Error, readFiles, options)
.then(event => {
addExceptionTypeValue(event, undefined, undefined);
addExceptionMechanism(event, mechanism);

resolve({
...event,
event_id: hint && hint.event_id,
});
})
.then(null, reject),
);
}

/**
* Builds and Event from a Message
* @hidden
*/
export function eventFromMessage(
options: NodeOptions,
message: string,
level: Severity = Severity.Info,
hint?: EventHint,
readFiles?: ReadFilesFn,
): PromiseLike<Event> {
const event: Event = {
event_id: hint && hint.event_id,
level,
message,
};

return new SyncPromise<Event>(resolve => {
if (options.attachStacktrace && hint && hint.syntheticException) {
const stack = hint.syntheticException ? extractStackFromError(hint.syntheticException) : [];
void parseStack(stack, readFiles, options)
.then(frames => {
event.stacktrace = {
frames: prepareFramesForEvent(frames),
};
resolve(event);
})
.then(null, () => {
resolve(event);
});
} else {
resolve(event);
}
});
}
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export { NodeOptions } from './types';
export { NodeBackend } from './backend';
export { NodeClient } from './client';
export { defaultIntegrations, init, lastEventId, flush, close, getSentryRelease } from './sdk';
export { eventFromException, eventFromMessage } from './eventbuilder';
export { deepReadDirSync } from './utils';
export { SDK_NAME } from './version';

Expand Down
Loading