-
Notifications
You must be signed in to change notification settings - Fork 5
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
Standardize our approach to handling API errors #502
Comments
I use this pattern a lot, and I don't love it: const resp = await fetch(endpoint, options).catch(console.error);
if (!resp) {
throw new Error("API unavailable");
}
if (isErrorCode(resp.status)) {
const { data } = await resp.json();
throw new Error(data);
}
return resp.json(); The problem is that we can get error info from the API but this isn't a good way to pass it to the frontend. |
Bigger thinking on error handling: The API will return error codes as a way of telling us we've done something wrong. These aren't actually "errors" in the Javascript sense, though. They shouldn't be raised and caught, in the same way we would if the API is down. This is similar to The API is actually pretty good about this, but I've been ignoring it. Here's the relevant line from the docs:
So we need to treat each 400-type response differently, and we need to do it later in the processing stack. To that end, I made the start of an export interface APIError {
error: {
status: number;
message: string;
};
} This probably isn't the final version of this, but it captures my basic thinking. We need to return an object from API methods that has a distinct shape -- we can check for an Most of the actual handling will end up in either routes or forms. For 404s, we can use SvelteKit's |
It makes sense to me and is a great start.
|
Shopify has a good example of success and error responses: https://shopify.dev/docs/api/admin-graphql#rate_limits |
Another thing that's tricky about error handling (or maybe makes things less tricky): Some API methods can just fail silently, and we're ok with that. A lot of the account-related methods are like that. If we can't get an authenticated user, we return Really, this is an issue with forms and permissions. You might try to do something, and if you hit an error, we want to tell you how to fix it. Maybe that limits the scope of what we need to address here. |
Sentry Issue: DOCUMENTCLOUD-FRONTEND-NEXT-H |
This is something that #704 may also help with—we'll get much better flagging if we assume |
Originally posted by @allanlasser in #494 (comment)
There are a bunch of places where we might hit an error related to the API. Right now, there are a bunch of bespoke handlers and lots of places that say something like
todo: handle this error better
. Let's think of a systematic approach.The text was updated successfully, but these errors were encountered: