Skip to content

fetchUrl throws generic error, discarding specific error details from response #159

@0xpatrickdev

Description

@0xpatrickdev

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:

{"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:

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;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions