hyperweb-io / hyperweb-io/chain-registry

`fetchUrl` throws generic error, discarding specific error details from response

Open
#159 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
70
Forks
39
PR merge metrics
No merged PRs in 30d

Description

When using `@chain-registry/client` via `starshipjs`, I observed a generic `Bad response` error and had trouble tracking down the root cause of an issue.

I came to learn the starship registry service returned a helpful error message:

```json
{"code":2, "message":"not found: no ibc connection found between chainA and chainB", "details":[]}
```

But it was swallowed by the client:
https://github.com/cosmology-tech/chain-registry/blob/b3b39185825b46807816e07d427935c56938923a/v1/packages/client/src/fetcher.ts#L13-L20

https://github.com/cosmology-tech/chain-registry/blob/b3b39185825b46807816e07d427935c56938923a/v2/packages/client/src/fetcher.ts#L26-L33

Changes along these lines should surface reported errors:
```ts
const fetchUrl = async (url: string) => {
const res = await fetch(url);
const data = await res.json();

if (!res.ok) {
// If the response contains error information, use it
if (data && data.code !== undefined && data.message) {
const error = new Error(data.message) as Error & {
code?: number;
details?: any[];
};
error.code = data.code;
error.details = data.details;
throw error;
} else {
throw new Error(`Bad Response: ${res.status}`);
}
}

return data;
};
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.