Skip to content

Commit

Permalink
Add EnokiClientError to expose enoki service error details (MystenLab…
Browse files Browse the repository at this point in the history
…s#16362)

## Description 

Describe the changes or additions included in this PR.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and do not break anything, you can
skip the following section. Otherwise, please briefly describe what has
changed under the Release Notes section.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
hayes-mysten authored Mar 2, 2024
1 parent 417671d commit f704211
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/plenty-cobras-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/enoki': patch
---

Add EnokiClientError to expose error details
24 changes: 23 additions & 1 deletion sdk/enoki/src/EnokiClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ export interface EnokiClientConfig {
apiUrl?: string;
}

export class EnokiClientError extends Error {
errors: { code: string; message: string; data: unknown }[] = [];

constructor(status: number, response: string) {
let errors;
try {
const parsedResponse = JSON.parse(response) as {
errors: { code: string; message: string; data: unknown }[];
};
errors = parsedResponse.errors;
} catch (e) {
// Ignore
}
const cause = errors?.[0] ? new Error(errors[0].message) : undefined;
super(`Request to Enoki API failed (status: ${status})`, {
cause,
});
this.errors = errors ?? [];
this.name = 'EnokiClientError';
}
}

/**
* A low-level client for interacting with the Enoki API.
*/
Expand Down Expand Up @@ -119,7 +141,7 @@ export class EnokiClient {
});

if (!res.ok) {
throw new Error('Failed to fetch');
throw new EnokiClientError(res.status, await res.text());
}

const { data } = await res.json();
Expand Down

0 comments on commit f704211

Please sign in to comment.