Skip to content
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

Add non-throwing variants #88

Open
maxdeviant opened this issue Sep 2, 2022 · 1 comment
Open

Add non-throwing variants #88

maxdeviant opened this issue Sep 2, 2022 · 1 comment

Comments

@maxdeviant
Copy link

I was very surprised to discover that getReasonPhrase throws an exception when given an HTTP status code that the library does not know about.

Could we add non-throwing variants of getReasonPhrase and getStatusCode that return undefined instead of throwing an exception?

Having to catch the exception just to handle these cases gracefully is not very ergonomic.

I would be willing to open a PR for this if it would be accepted.

@religiosa1
Copy link

I think a fallback can be used, as the second argument of getReasonPhrase.

If a fallback is provided, it will be used as the return value if no matching code is found.
Otherwise, an exception will be thrown as usual.

This approach is backward compatible and covers probably the most common use case of trying to get a phrase from a code or using a specified alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants