Skip to content

Conversation

@clicktodev
Copy link
Contributor

@clicktodev clicktodev commented Sep 25, 2025

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

@clicktodev
Copy link
Contributor Author

I'm thinking about changing the check to isTimeoutError: (error: unknown) => error is TimeoutError || error.name === HTTPError.name; to handle cross realm situations

@clicktodev
Copy link
Contributor Author

last commit adds a fallback name check in case instanceof fails for some reason. let me know what you think

@clicktodev
Copy link
Contributor Author

also idk if you want the helpers to be attached to the ky instance or keep them standalone

@sholladay
Copy link
Collaborator

IMO, these functions should be standalone and not attached to the Ky instance.

I also think that, unlike isKyError(), the more specific functions provide limited value and probably shouldn't be included.

@clicktodev
Copy link
Contributor Author

clicktodev commented Sep 26, 2025

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.
I'm thinking of passing a boolean parameter to enable the name check which defaults to false, we can call it crossRealmCheck or nameCheck

export function isHTTPError<T = unknown>(error: unknown, crossRealmCheck = false): error is HTTPError<T> {
    return error instanceof HTTPError || (crossRealmCheck && ((error as any)?.name === HTTPError.name));
}

@clicktodev
Copy link
Contributor Author

i removed the name check for now but i can bring it back if you think it's useful

@sholladay
Copy link
Collaborator

They should support name as a fallback. Otherwise they don't earn their keep. Needs to be worth the weight of an import.

@clicktodev
Copy link
Contributor Author

My only issue is that name checking can produce false positives since other errors can potentially have the same name.
Should we still go through with the name check?

@clicktodev
Copy link
Contributor Author

brought back the name checks

@sholladay
Copy link
Collaborator

Just one more small nit. Otherwise LGTM.

@clicktodev clicktodev requested a review from sholladay October 3, 2025 22:45
@sholladay
Copy link
Collaborator

The imports can be combined into a single line:

import ky, {TimeoutError} from 'ky';

@clicktodev clicktodev requested a review from sholladay October 6, 2025 08:15
@sholladay sholladay changed the title Add type guards for error handling in Ky client Add type guards for errors Oct 6, 2025
@sholladay sholladay merged commit 7e1fd0b into sindresorhus:main Oct 6, 2025
3 checks passed
@clicktodev clicktodev deleted the feature/type-guards branch October 6, 2025 13:21
@clicktodev
Copy link
Contributor Author

clicktodev commented Nov 30, 2025

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.

@sholladay
Copy link
Collaborator

sholladay commented Nov 30, 2025

I don't see how that would be useful here. Error.isError() does not support comparisons to check if an object is one of our errors.

@clicktodev
Copy link
Contributor Author

You're right but we can however use it to get rid of the as any type assertion.

(Error.isError(error) && error.name === HttpError.name);

instead of

((error as any)?.name === HTTPError.name);

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 : (

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

Successfully merging this pull request may close these issues.

feat: type guard for ky errors

2 participants