http-status-codes
http-status-codes copied to clipboard
Add non-throwing variants
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.
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.