-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
Allow for logging network errors and JSON parse errors #605
Comments
Let's start by adding Regarding network errors, indeed a lot of people have requested similar functionality. A major version bump is no problem, one is likely coming soon anyway because of PR #602. However, we are not yet in a great place to provide this feature for a few reasons:
All that being said, I will happily review any PR you send. Just don't be surprised if we decide not to move forward with it. 🙂 |
@sholladay thanks for the super fast response 🙇 Couple of thoughts on your points:
Based on this it feels like trying to read into what went wrong when try {
const response = await fetch(url);
} catch (e) { // e is typed to any or unknown based on TS config
if (e instanceof TypeError) {
// probably a network error
} else if (e instanceof Error && e.name === "AbortError") {
// client aborted
} else if (e instanceof Error) {
// some random unknown error
} else {
// ok something really strange has happened
}
} I think it's fair to expect consumers to perform similar error handling with type BeforeNetworkErrorState = {
request: Request;
options: NormalizedOptions;
error: unknown; // type as unknown so consumers can do their own type narrowing
};
const onFetchError: OnFetchErrorHook = ({ error, request, response, options }) => {
if (error instanceof TypeError) {
// probably a network error
} else if (error instanceof Error && error.name === "AbortError") {
// client aborted
} else if (error instanceof Error) {
// some random unknown error
} else {
// ok something really strange has happened
}
} Consumers are able to use
I agree that it's not a good idea to allow handling of fetch errors because it would cause problems upstream. Perhaps having a naming convention of the hooks could set expectations about that:
So in this case |
You're welcome!
There's definitely a line to be drawn somewhere there. I think its perfectly reasonable to expect Ky to wallpaper over some of
That's what I want to do. We'll expose the
That sounds pretty reasonable. We could even allow users to |
In my case I'd like to use a shared api client across multiple different apps. Sometimes directly making fetch requests, sometimes inside React Query where errors are caught automatically. I'd like to have one consistent way of logging API errors everywhere. To me it makes sense to do it centrally otherwise it'll be hard to maintain consistent logging everywhere. This discussion is making me wonder if perhaps the problem of logging is different to what It could take a stream of events that are typed as a discriminated union so you could add different events over time without breaking changes. type LogEvent =
{ type: "RequestStart"; request: Request; options: Options }
| { type: "ResponseSuccess"; request: Request; response: Response; options: Options }
| { type: "ResponseError"; request: Request; error: Error; options: Options }
| { type: "JsonParseError"; request: Request; error: Error; options: Options };
type Logger = (event: LogEvent) => void; |
I like that API. It might have too much overlap with hooks for both APIs to exist, but I'll give it some thought. For now, let's create the |
@sholladay are you folks looking for someone to pick up this enhancement? I'd like to help out here if it's possible! |
Hi! I'd like to contribute to
ky
for selfish reasons. I want to use it in my org's production app however, after trying to hook up the necessary logging we need I've come to a dead end.The logging that I need is:
beforeRequest
hook works for this)afterResponse
works for success and HTTPError responses but not Network Errors)Response.json()
failsSo that leaves me with two gaps:
Response.json()
errorsI'd like to get a rough steer on an approach for these both before creating a PR.
Hooking into
Response.json()
errorsMy first attempt at this was to try to do it in my own
parseJson
method:The problem with this approach is that, within the context of
parseJson
you don't have access to the request context so you can't send any information about the specific request along with the error.Possible solutions
I can think of two sensible options:
1. Pass the request information to the
parseJson
optionThis would look something like:
not sure exactly what you'd want to pass in that second argument, could potentially contain the response as well if that's feasible.
2. A new hook -
beforeJsonParseError
This would be more fitting with how we can hook into HTTP Errors.
Let me know if either of those sound reasonable or if you have any other ideas.
Hooking into network errors
This is already covered by another issue: #296
With this one I'm not sure if it's worth creating a new hook or expanding the
beforeError
hook to also run on network errors. ModifyingbeforeError
behaviour would probably require a major version bump as it could be a breaking change but it does feel like the more sensible API.otherwise I could add a new
beforeNetworkError
hookAgain, please let me know if either of those sound reasonable or if you have any other ideas.
Thanks!
The text was updated successfully, but these errors were encountered: