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

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

Open
0xpatrickdev opened this issue Oct 17, 2024 · 0 comments

Comments

@0xpatrickdev
Copy link

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:

const fetchUrl = (url: string) => {
return fetch(url).then((res) => {
if (res.status >= 400) {
throw new Error('Bad response');
}
return res.json();
});
};

const fetchUrl = (url: string) => {
return fetch(url).then((res) => {
if (res.status >= 400) {
throw new Error('Bad response');
}
return res.json();
});
};

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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant