Skip to content

Commit

Permalink
fix: catch variant/flag fetch timeout error and log at debug-level (#135
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tyiuhc authored Oct 30, 2024
1 parent 05c2c38 commit 879cfe3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
30 changes: 22 additions & 8 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Poller,
SdkEvaluationApi,
SdkFlagApi,
TimeoutError,
topologicalSort,
} from '@amplitude/experiment-core';

Expand Down Expand Up @@ -255,7 +256,11 @@ export class ExperimentClient implements Client {
);
} catch (e) {
if (this.config.debug) {
console.error(e);
if (e instanceof TimeoutError) {
console.debug(e);
} else {
console.error(e);
}
}
}
return this;
Expand Down Expand Up @@ -697,13 +702,22 @@ export class ExperimentClient implements Client {
}

private async doFlags(): Promise<void> {
const flags = await this.flagApi.getFlags({
libraryName: 'experiment-js-client',
libraryVersion: PACKAGE_VERSION,
timeoutMillis: this.config.fetchTimeoutMillis,
});
this.flags.clear();
this.flags.putAll(flags);
try {
const flags = await this.flagApi.getFlags({
libraryName: 'experiment-js-client',
libraryVersion: PACKAGE_VERSION,
timeoutMillis: this.config.fetchTimeoutMillis,
});
this.flags.clear();
this.flags.putAll(flags);
} catch (e) {
if (this.config.debug && e instanceof TimeoutError) {
console.debug(e);
} else {
throw e;
}
}

try {
this.flags.store();
} catch (e) {
Expand Down
9 changes: 7 additions & 2 deletions packages/experiment-browser/src/transport/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @internal
*/

import { safeGlobal } from '@amplitude/experiment-core';
import { safeGlobal, TimeoutError } from '@amplitude/experiment-core';
import {
HttpClient as CoreHttpClient,
HttpRequest,
Expand All @@ -29,7 +29,11 @@ const timeout = (
}
return new Promise(function (resolve, reject) {
safeGlobal.setTimeout(function () {
reject(Error('Request timeout after ' + timeoutMillis + ' milliseconds'));
reject(
new TimeoutError(
'Request timeout after ' + timeoutMillis + ' milliseconds',
),
);
}, timeoutMillis);
promise.then(resolve, reject);
});
Expand Down Expand Up @@ -63,6 +67,7 @@ const _request = (
*/
export class WrapperClient implements CoreHttpClient {
private readonly client: HttpClient;

constructor(client: HttpClient) {
this.client = client;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/experiment-core/src/evaluation/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ export class FetchError extends Error {
Object.setPrototypeOf(this, FetchError.prototype);
}
}

export class TimeoutError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, TimeoutError.prototype);
}
}
2 changes: 1 addition & 1 deletion packages/experiment-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ export {
getGlobalScope,
isLocalStorageAvailable,
} from './util/global';
export { FetchError } from './evaluation/error';
export { FetchError, TimeoutError } from './evaluation/error';

0 comments on commit 879cfe3

Please sign in to comment.