-
-
Notifications
You must be signed in to change notification settings - Fork 363
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
Ensure HTTPError always has a defined message #249
Conversation
This would also help with Sentry error reporting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the implementation but for the tests, I would prefer to use more realistic objects and patterns that align better with the other existing tests. For example, use an actual test server that responds without status text. Or instead of http.ServerResponse
, return a WHATWG Response
inside of a hook.
Thanks for the quick review!
Yeah, I struggled with this a bit. I couldn't see an easy way to make a response with an undefined
Ah, yes, a WHATWG |
@sholladay I switched to a proper fetch |
@sholladay this supports status codes of 0 now. |
// Assign these values because the Response() constructor doesn't | ||
// support setting them to undefined. | ||
response.status = status; | ||
response.statusText = statusText; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can be set in the init
argument for the Response
constructor. Should be eas easy as...
const error = new ky.HTTPError(new Response(null, { status: 200, statusText: 'Foo bar' }));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work because these values need to be set to undefined
and from looking at the node-fetch
source, they are using old school truthy checks to tell if these values are set (thus, they can never be set to undefined
).
I realize it makes the code look a bit odd -- that's why I added a comment explaining it.
@sholladay let me know if my response makes sense. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Might be nice, though, to add parentheses around the OR condition (... || ...)
to make it clear that it has higher precedence than the ternary.
Fixes #248
This ensures HTTPError will always have a defined message in various situations.