-
-
Notifications
You must be signed in to change notification settings - Fork 444
Add type guards for errors #746
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
Conversation
|
I'm thinking about changing the check to |
…rror (cross realm support)
|
last commit adds a fallback name check in case instanceof fails for some reason. let me know what you think |
|
also idk if you want the helpers to be attached to the ky instance or keep them standalone |
|
IMO, these functions should be standalone and not attached to the Ky instance. I also think that, unlike |
|
i think the specific type guards are beneficial for both internal and external use. if (isKyError(error)) {
if (isHTTPError(error)) {
console.log('Status:', error.response.status);
} else if (isTimeoutError(error)) {
console.log('Timeout URL:', error.request.url);
}
} I also replaced the internal instanceof checks with the new helper functions. export function isHTTPError<T = unknown>(error: unknown, crossRealmCheck = false): error is HTTPError<T> {
return error instanceof HTTPError || (crossRealmCheck && ((error as any)?.name === HTTPError.name));
} |
|
i removed the name check for now but i can bring it back if you think it's useful |
|
They should support |
|
My only issue is that name checking can produce false positives since other errors can potentially have the same name. |
|
brought back the name checks |
|
Just one more small nit. Otherwise LGTM. |
|
The imports can be combined into a single line: import ky, {TimeoutError} from 'ky'; |
|
it seems node 24 introduces Error.isError() (still not fully supported in safari) which works cross realm which means we can get rid of the name check and the instanceof check. |
|
I don't see how that would be useful here. |
|
You're right but we can however use it to get rid of the
instead of
but this requires bumping to node 24 but i don't think it's worth it until node 24 becomes the lowest maintenance release. also until safari fixes its implementation because it currently returns false on DomExceptions : ( |
Introduce type guards to enhance error handling in the Ky client, allowing for more precise identification of HTTP and timeout errors. This improves the robustness of error management in the application.
closes #742