Skip to content

Commit 879cfe3

Browse files
authored
fix: catch variant/flag fetch timeout error and log at debug-level (#135)
1 parent 05c2c38 commit 879cfe3

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

packages/experiment-browser/src/experimentClient.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Poller,
1313
SdkEvaluationApi,
1414
SdkFlagApi,
15+
TimeoutError,
1516
topologicalSort,
1617
} from '@amplitude/experiment-core';
1718

@@ -255,7 +256,11 @@ export class ExperimentClient implements Client {
255256
);
256257
} catch (e) {
257258
if (this.config.debug) {
258-
console.error(e);
259+
if (e instanceof TimeoutError) {
260+
console.debug(e);
261+
} else {
262+
console.error(e);
263+
}
259264
}
260265
}
261266
return this;
@@ -697,13 +702,22 @@ export class ExperimentClient implements Client {
697702
}
698703

699704
private async doFlags(): Promise<void> {
700-
const flags = await this.flagApi.getFlags({
701-
libraryName: 'experiment-js-client',
702-
libraryVersion: PACKAGE_VERSION,
703-
timeoutMillis: this.config.fetchTimeoutMillis,
704-
});
705-
this.flags.clear();
706-
this.flags.putAll(flags);
705+
try {
706+
const flags = await this.flagApi.getFlags({
707+
libraryName: 'experiment-js-client',
708+
libraryVersion: PACKAGE_VERSION,
709+
timeoutMillis: this.config.fetchTimeoutMillis,
710+
});
711+
this.flags.clear();
712+
this.flags.putAll(flags);
713+
} catch (e) {
714+
if (this.config.debug && e instanceof TimeoutError) {
715+
console.debug(e);
716+
} else {
717+
throw e;
718+
}
719+
}
720+
707721
try {
708722
this.flags.store();
709723
} catch (e) {

packages/experiment-browser/src/transport/http.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @internal
44
*/
55

6-
import { safeGlobal } from '@amplitude/experiment-core';
6+
import { safeGlobal, TimeoutError } from '@amplitude/experiment-core';
77
import {
88
HttpClient as CoreHttpClient,
99
HttpRequest,
@@ -29,7 +29,11 @@ const timeout = (
2929
}
3030
return new Promise(function (resolve, reject) {
3131
safeGlobal.setTimeout(function () {
32-
reject(Error('Request timeout after ' + timeoutMillis + ' milliseconds'));
32+
reject(
33+
new TimeoutError(
34+
'Request timeout after ' + timeoutMillis + ' milliseconds',
35+
),
36+
);
3337
}, timeoutMillis);
3438
promise.then(resolve, reject);
3539
});
@@ -63,6 +67,7 @@ const _request = (
6367
*/
6468
export class WrapperClient implements CoreHttpClient {
6569
private readonly client: HttpClient;
70+
6671
constructor(client: HttpClient) {
6772
this.client = client;
6873
}

packages/experiment-core/src/evaluation/error.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ export class FetchError extends Error {
77
Object.setPrototypeOf(this, FetchError.prototype);
88
}
99
}
10+
11+
export class TimeoutError extends Error {
12+
constructor(message: string) {
13+
super(message);
14+
Object.setPrototypeOf(this, TimeoutError.prototype);
15+
}
16+
}

packages/experiment-core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ export {
2323
getGlobalScope,
2424
isLocalStorageAvailable,
2525
} from './util/global';
26-
export { FetchError } from './evaluation/error';
26+
export { FetchError, TimeoutError } from './evaluation/error';

0 commit comments

Comments
 (0)