Skip to content

Commit 06e3319

Browse files
authored
chore: improve error handing for fetch function (#267)
Signed-off-by: Mirko Mollik <[email protected]>
1 parent 3c85e8e commit 06e3319

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/sd-jwt-vc/src/sd-jwt-vc-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ export type SDJWTVCConfig = SDJWTConfig & {
1616
vctFetcher?: VcTFetcher;
1717
// if set to true, it will load the metadata format based on the vct value. If not provided, it will default to false.
1818
loadTypeMetadataFormat?: boolean;
19+
// timeout value in milliseconds when to abort the fetch request. If not provided, it will default to 10000.
20+
timeout?: number;
1921
};

packages/sd-jwt-vc/src/sd-jwt-vc-instance.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,31 @@ export class SDJwtVcInstance extends SDJwtInstance<SdJwtVcPayload> {
166166
* @param url
167167
* @returns
168168
*/
169-
private async fetch<T>(url: string, integrity?: string) {
169+
private async fetch<T>(url: string, integrity?: string): Promise<T> {
170170
const controller = new AbortController();
171-
const timeoutId = setTimeout(() => controller.abort(), 10000);
171+
const timeoutId = setTimeout(
172+
() => controller.abort(),
173+
this.userConfig.timeout ?? 10000,
174+
);
175+
172176
try {
173177
const response = await fetch(url, {
174178
signal: controller.signal,
175179
});
180+
176181
if (!response.ok) {
177-
throw new Error(await response.text());
182+
const errorText = await response.text();
183+
throw new Error(
184+
`Error fetching ${url}: ${response.status} ${response.statusText} - ${errorText}`,
185+
);
178186
}
179187
await this.validateIntegrity(response.clone(), url, integrity);
180188
return response.json() as Promise<T>;
189+
} catch (error) {
190+
if (error instanceof DOMException && error.name === 'AbortError') {
191+
throw new Error(`Request to ${url} timed out`);
192+
}
193+
throw error;
181194
} finally {
182195
clearTimeout(timeoutId);
183196
}

0 commit comments

Comments
 (0)