http-status-codes icon indicating copy to clipboard operation
http-status-codes copied to clipboard

Add non-throwing variants

Open maxdeviant opened this issue 2 years ago • 1 comments

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.

maxdeviant avatar Sep 02 '22 14:09 maxdeviant

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.

religiosa1 avatar Feb 05 '23 12:02 religiosa1