Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve error handing for fetch function #267

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/sd-jwt-vc/src/sd-jwt-vc-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export type SDJWTVCConfig = SDJWTConfig & {
vctFetcher?: VcTFetcher;
// if set to true, it will load the metadata format based on the vct value. If not provided, it will default to false.
loadTypeMetadataFormat?: boolean;
// timeout value in milliseconds when to abort the fetch request. If not provided, it will default to 10000.
timeout?: number;
};
19 changes: 16 additions & 3 deletions packages/sd-jwt-vc/src/sd-jwt-vc-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,31 @@ export class SDJwtVcInstance extends SDJwtInstance<SdJwtVcPayload> {
* @param url
* @returns
*/
private async fetch<T>(url: string, integrity?: string) {
private async fetch<T>(url: string, integrity?: string): Promise<T> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000);
const timeoutId = setTimeout(
() => controller.abort(),
this.userConfig.timeout ?? 10000,
);

try {
const response = await fetch(url, {
signal: controller.signal,
});

if (!response.ok) {
throw new Error(await response.text());
const errorText = await response.text();
throw new Error(
`Error fetching ${url}: ${response.status} ${response.statusText} - ${errorText}`,
);
}
await this.validateIntegrity(response.clone(), url, integrity);
return response.json() as Promise<T>;
} catch (error) {
if (error instanceof DOMException && error.name === 'AbortError') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In node.js or react native environments, is this still a DOMException?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. It could be a problem in other platform. I missed exception handling part.
@cre8 I think we should find other way to handle it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@berendsliedrecht @lukasjhan I think we should write a test and find it out. I added this instance of check, otherwise the linter would fail because of the missing type.

I will open a new PR to add tests and to correct it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@berendsliedrecht I ran the test in node pnpm run test:node and it threw me the DomException. I am not sure how it will work with react native since we have no chance here to test it...

One solution would be to replace fetch via a http library like axios. But this will add another external lib

throw new Error(`Request to ${url} timed out`);
}
throw error;
} finally {
clearTimeout(timeoutId);
}
Expand Down
Loading