Skip to content

Commit f6fc304

Browse files
authored
feat: Add flush & close and read dsn of process.env for node (#1892)
* feat: Add flush close and dsn from process for node * feat: Expose flush and close also on browser * fix: Lint error and add changelog
1 parent 21a449b commit f6fc304

File tree

8 files changed

+72
-8
lines changed

8 files changed

+72
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
- [node] feat: If `options.dsn` is undefined when calling `init` we try to load it from `process.env.SENTRY_DSN`
6+
- [node] feat: Expose `flush` and `close` on `Sentry.*`
7+
- [node] feat: Add `sentry` to express error handler response which contains the `event_id` of the error
8+
- [core] feat: Introduce `flush` method which currently is an alias for `close`
9+
510
## 4.5.4
611

712
- [browser] fix: `DOMError` and `DOMException` should be error level events
@@ -14,10 +19,10 @@
1419
## 4.5.3
1520

1621
- [browser]: fix: Fix UnhandledPromise: [object Object]
17-
- [core]: fix: Error in extraErrorData integration where event would not be send in case of non assignable object property.
22+
- [core]: fix: Error in extraErrorData integration where event would not be send in case of non assignable object
23+
property.
1824
- [hub]: feat: Support non async event processors
1925

20-
2126
## 4.5.2
2227

2328
- [utils] fix: Decycling for objects to no produce an endless loop

packages/browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export {
2929

3030
export { BrowserBackend, BrowserOptions } from './backend';
3131
export { BrowserClient, ReportDialogOptions } from './client';
32-
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog } from './sdk';
32+
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close } from './sdk';
3333
export { SDK_NAME, SDK_VERSION } from './version';
3434

3535
import { Integrations as CoreIntegrations } from '@sentry/core';

packages/browser/src/sdk.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,23 @@ export function forceLoad(): void {
103103
export function onLoad(callback: () => void): void {
104104
callback();
105105
}
106+
107+
/**
108+
* A promise that resolves when all current events have been sent.
109+
* If you provide a timeout and the queue takes longer to drain the promise returns false.
110+
*
111+
* @param timeout Maximum time in ms the client should wait.
112+
*/
113+
export async function flush(timeout?: number): Promise<boolean> {
114+
return (getCurrentHub().getClient() as BrowserClient).flush(timeout);
115+
}
116+
117+
/**
118+
* A promise that resolves when all current events have been sent.
119+
* If you provide a timeout and the queue takes longer to drain the promise returns false.
120+
*
121+
* @param timeout Maximum time in ms the client should wait.
122+
*/
123+
export async function close(timeout?: number): Promise<boolean> {
124+
return (getCurrentHub().getClient() as BrowserClient).close(timeout);
125+
}

packages/core/src/baseclient.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
386386
/**
387387
* @inheritDoc
388388
*/
389-
public async close(timeout?: number): Promise<boolean> {
389+
public async flush(timeout?: number): Promise<boolean> {
390390
return (await Promise.all([
391391
this.getBackend()
392392
.getTransport()
@@ -395,6 +395,13 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
395395
])).reduce((prev, current) => prev && current);
396396
}
397397

398+
/**
399+
* @inheritDoc
400+
*/
401+
public async close(timeout?: number): Promise<boolean> {
402+
return this.flush(timeout);
403+
}
404+
398405
/**
399406
* @inheritDoc
400407
*/

packages/core/src/interfaces.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,21 @@ export interface Client<O extends Options = Options> {
207207
getOptions(): O;
208208

209209
/**
210-
* A promise that resolves whenever the request buffer is empty.
211-
* If you provide a timeout and the buffer takes longer to drain the promise returns false.
210+
* A promise that resolves when all current events have been sent.
211+
* If you provide a timeout and the queue takes longer to drain the promise returns false.
212212
*
213213
* @param timeout Maximum time in ms the client should wait.
214214
*/
215215
close(timeout?: number): Promise<boolean>;
216216

217+
/**
218+
* A promise that resolves when all current events have been sent.
219+
* If you provide a timeout and the queue takes longer to drain the promise returns false.
220+
*
221+
* @param timeout Maximum time in ms the client should wait.
222+
*/
223+
flush(timeout?: number): Promise<boolean>;
224+
217225
/** Returns an array of installed integrations on the client. */
218226
getIntegration<T extends Integration>(integartion: IntegrationClass<T>): T | null;
219227
}

packages/node/src/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export function errorHandler(): (
272272
return;
273273
}
274274
const eventId = captureException(error);
275-
_res.sentry = eventId;
275+
(_res as any).sentry = eventId;
276276
next(error);
277277
};
278278
}

packages/node/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export {
2929

3030
export { NodeBackend, NodeOptions } from './backend';
3131
export { NodeClient } from './client';
32-
export { defaultIntegrations, init } from './sdk';
32+
export { defaultIntegrations, init, flush, close } from './sdk';
3333
export { SDK_NAME, SDK_VERSION } from './version';
3434

3535
import { Integrations as CoreIntegrations } from '@sentry/core';

packages/node/src/sdk.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export function init(options: NodeOptions = {}): void {
7070
options.defaultIntegrations = defaultIntegrations;
7171
}
7272

73+
if (options.dsn === undefined && process.env.SENTRY_DSN) {
74+
options.dsn = process.env.SENTRY_DSN;
75+
}
76+
7377
if (domain.active) {
7478
setHubOnCarrier(getMainCarrier(), getCurrentHub());
7579
}
@@ -85,3 +89,23 @@ export function init(options: NodeOptions = {}): void {
8589
export function lastEventId(): string | undefined {
8690
return getCurrentHub().lastEventId();
8791
}
92+
93+
/**
94+
* A promise that resolves when all current events have been sent.
95+
* If you provide a timeout and the queue takes longer to drain the promise returns false.
96+
*
97+
* @param timeout Maximum time in ms the client should wait.
98+
*/
99+
export async function flush(timeout?: number): Promise<boolean> {
100+
return (getCurrentHub().getClient() as NodeClient).flush(timeout);
101+
}
102+
103+
/**
104+
* A promise that resolves when all current events have been sent.
105+
* If you provide a timeout and the queue takes longer to drain the promise returns false.
106+
*
107+
* @param timeout Maximum time in ms the client should wait.
108+
*/
109+
export async function close(timeout?: number): Promise<boolean> {
110+
return (getCurrentHub().getClient() as NodeClient).close(timeout);
111+
}

0 commit comments

Comments
 (0)