Skip to content

Commit b89b086

Browse files
authored
fix(core): Stop rejecting in flush and close when client undefined (#3846)
Prior to this change, if the SDK had no client defined, calling `Sentry.close()` or `Sentry.flush()` would result in a promise rejection. Since this is not an error per se, this changes that rejection to a logged warning and a promise resolution with `false`. It also fleshes out the docstrings on all of the downstream methods (all of which essentially do the same thing - wait for the event queue to be empty, or timeout if it takes too long). Fixes #3031.
1 parent 74a0829 commit b89b086

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

packages/browser/src/sdk.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,37 @@ export function onLoad(callback: () => void): void {
146146
}
147147

148148
/**
149-
* A promise that resolves when all current events have been sent.
150-
* If you provide a timeout and the queue takes longer to drain the promise returns false.
149+
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
151150
*
152-
* @param timeout Maximum time in ms the client should wait.
151+
* @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
152+
* the client to wait until all events are sent before resolving the promise.
153+
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
154+
* doesn't (or if there's no client defined).
153155
*/
154156
export function flush(timeout?: number): PromiseLike<boolean> {
155157
const client = getCurrentHub().getClient<BrowserClient>();
156158
if (client) {
157159
return client.flush(timeout);
158160
}
159-
return SyncPromise.reject(false);
161+
logger.warn('Cannot flush events. No client defined.');
162+
return SyncPromise.resolve(false);
160163
}
161164

162165
/**
163-
* A promise that resolves when all current events have been sent.
164-
* If you provide a timeout and the queue takes longer to drain the promise returns false.
166+
* Call `close()` on the current client, if there is one. See {@link Client.close}.
165167
*
166-
* @param timeout Maximum time in ms the client should wait.
168+
* @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
169+
* parameter will cause the client to wait until all events are sent before disabling itself.
170+
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
171+
* doesn't (or if there's no client defined).
167172
*/
168173
export function close(timeout?: number): PromiseLike<boolean> {
169174
const client = getCurrentHub().getClient<BrowserClient>();
170175
if (client) {
171176
return client.close(timeout);
172177
}
173-
return SyncPromise.reject(false);
178+
logger.warn('Cannot flush events and disable SDK. No client defined.');
179+
return SyncPromise.resolve(false);
174180
}
175181

176182
/**

packages/node/src/sdk.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
22
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
33
import { SessionStatus } from '@sentry/types';
4-
import { getGlobalObject } from '@sentry/utils';
4+
import { getGlobalObject, logger } from '@sentry/utils';
55
import * as domain from 'domain';
66

77
import { NodeClient } from './client';
@@ -140,31 +140,37 @@ export function lastEventId(): string | undefined {
140140
}
141141

142142
/**
143-
* A promise that resolves when all current events have been sent.
144-
* If you provide a timeout and the queue takes longer to drain the promise returns false.
143+
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
145144
*
146-
* @param timeout Maximum time in ms the client should wait.
145+
* @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
146+
* the client to wait until all events are sent before resolving the promise.
147+
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
148+
* doesn't (or if there's no client defined).
147149
*/
148150
export async function flush(timeout?: number): Promise<boolean> {
149151
const client = getCurrentHub().getClient<NodeClient>();
150152
if (client) {
151153
return client.flush(timeout);
152154
}
153-
return Promise.reject(false);
155+
logger.warn('Cannot flush events. No client defined.');
156+
return Promise.resolve(false);
154157
}
155158

156159
/**
157-
* A promise that resolves when all current events have been sent.
158-
* If you provide a timeout and the queue takes longer to drain the promise returns false.
160+
* Call `close()` on the current client, if there is one. See {@link Client.close}.
159161
*
160-
* @param timeout Maximum time in ms the client should wait.
162+
* @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
163+
* parameter will cause the client to wait until all events are sent before disabling itself.
164+
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
165+
* doesn't (or if there's no client defined).
161166
*/
162167
export async function close(timeout?: number): Promise<boolean> {
163168
const client = getCurrentHub().getClient<NodeClient>();
164169
if (client) {
165170
return client.close(timeout);
166171
}
167-
return Promise.reject(false);
172+
logger.warn('Cannot flush events and disable SDK. No client defined.');
173+
return Promise.resolve(false);
168174
}
169175

170176
/**

packages/types/src/client.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ export interface Client<O extends Options = Options> {
6060
getOptions(): O;
6161

6262
/**
63-
* A promise that resolves when all current events have been sent.
64-
* If you provide a timeout and the queue takes longer to drain the promise returns false.
63+
* Flush the event queue and set the client to `enabled = false`. See {@link Client.flush}.
6564
*
66-
* @param timeout Maximum time in ms the client should wait.
65+
* @param timeout Maximum time in ms the client should wait before shutting down. Omitting this parameter will cause
66+
* the client to wait until all events are sent before disabling itself.
67+
* @returns A promise which resolves to `true` if the flush completes successfully before the timeout, or `false` if
68+
* it doesn't.
6769
*/
6870
close(timeout?: number): PromiseLike<boolean>;
6971

7072
/**
71-
* A promise that resolves when all current events have been sent.
72-
* If you provide a timeout and the queue takes longer to drain the promise returns false.
73+
* Wait for all events to be sent or the timeout to expire, whichever comes first.
7374
*
74-
* @param timeout Maximum time in ms the client should wait.
75+
* @param timeout Maximum time in ms the client should wait for events to be flushed. Omitting this parameter will
76+
* cause the client to wait until all events are sent before resolving the promise.
77+
* @returns A promise that will resolve with `true` if all events are sent before the timeout, or `false` if there are
78+
* still events in the queue when the timeout is reached.
7579
*/
7680
flush(timeout?: number): PromiseLike<boolean>;
7781

packages/types/src/transport.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ export interface Transport {
2121
sendSession?(session: Session | SessionAggregates): PromiseLike<Response>;
2222

2323
/**
24-
* Call this function to wait until all pending requests have been sent.
24+
* Wait for all events to be sent or the timeout to expire, whichever comes first.
2525
*
26-
* @param timeout Number time in ms to wait until the buffer is drained.
26+
* @param timeout Maximum time in ms the transport should wait for events to be flushed. Omitting this parameter will
27+
* cause the transport to wait until all events are sent before resolving the promise.
28+
* @returns A promise that will resolve with `true` if all events are sent before the timeout, or `false` if there are
29+
* still events in the queue when the timeout is reached.
2730
*/
2831
close(timeout?: number): PromiseLike<boolean>;
2932
}

0 commit comments

Comments
 (0)