Skip to content

Commit dcef2ab

Browse files
committed
Load attachments from paths in node
1 parent 8d70b13 commit dcef2ab

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

packages/core/src/baseclient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
527527
* @param event The Sentry event to send
528528
*/
529529
// TODO(v7): refactor: get rid of method?
530-
protected _sendEvent(event: Event, attachments: AttachmentItem[]): void {
530+
protected _sendEvent(event: Event, attachments?: AttachmentItem[]): void {
531531
this.sendEvent(event, attachments);
532532
}
533533

@@ -658,8 +658,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
658658
}
659659

660660
/** */
661-
protected _getAttachments(scope: Scope | undefined): AttachmentItem[] {
662-
return (scope?.getAttachments() || []).map(a => createAttachmentEnvelopeItem(a));
661+
protected _getAttachments(scope: Scope | undefined): AttachmentItem[] | undefined {
662+
return scope?.getAttachments()?.map(a => createAttachmentEnvelopeItem(a));
663663
}
664664

665665
/**

packages/node/src/client.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
22
import { SessionFlusher } from '@sentry/hub';
33
import { AttachmentItem, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
4-
import { createAttachmentEnvelopeItem, logger, resolvedSyncPromise } from '@sentry/utils';
4+
import { basename, createAttachmentEnvelopeItem, logger, resolvedSyncPromise } from '@sentry/utils';
5+
import { existsSync, readFileSync } from 'fs';
56

67
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
78
import { IS_DEBUG_BUILD } from './flags';
@@ -154,8 +155,17 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
154155
/**
155156
* @inheritDoc
156157
*/
157-
protected _getAttachments(scope: Scope | undefined): AttachmentItem[] {
158-
// TODO: load any attachments from paths...
159-
return (scope?.getAttachments() || []).map(a => createAttachmentEnvelopeItem(a));
158+
protected _getAttachments(scope: Scope | undefined): AttachmentItem[] | undefined {
159+
return scope?.getAttachments()?.map(attachment => {
160+
let [pathOrData, options] = attachment;
161+
162+
if (typeof pathOrData === 'string' && existsSync(pathOrData)) {
163+
options = options || {};
164+
options.filename = basename(pathOrData);
165+
pathOrData = readFileSync(pathOrData);
166+
}
167+
168+
return createAttachmentEnvelopeItem([pathOrData, options]);
169+
});
160170
}
161171
}

0 commit comments

Comments
 (0)