Open tsamaya opened 10 months ago
An Idea from https://stackoverflow.com/a/47058425/1910637
In Spring you have exactly what you want:
HttpClientErrorException - Exception thrown when an HTTP 4xx is received. HttpServerErrorException - Exception thrown when an HTTP 5xx is received.
Minimally, you should differentiate exceptions related to business logic (e.g., insufficient balance, email address is not valid) from other exceptions (e.g., server not available, unsupported media type, SQLException).
In our REST API, we have a library for Java clients that parses responses and throws only three different exceptions:
400, 401, 403, 404, 409, 422: throw MyBusinessException, which contains a message that can be shown to the end user. The message comes in the response body (exception handling on the service side), but if not present we have a default message specific to each status code. 405, 412, 415: throw HttpClientErrorException with a message that is specific to each status code. other 4xx codes: throw HttpClientErrorException with a generic message. 5xx codes: throw HttpServerErrorException with a generic message. All these exceptions are unchecked.
Each HTTP Error code would have its own Exception, they all extend a base exception.
Following a discussion with @lpellegr, the exception names won't always reflect the transport used.
The base exception is going to be JOpenCageException.
PR #33 is merged into the release-3.0.0
branch and a snapshot version is available for testing on https://oss.sonatype.org/content/repositories/snapshots/com/opencagedata/jopencage/3.0.0-SNAPSHOT/
NB: Keeping this issue open up to the v3 release
The package logs the error and returns null. Structuring the exceptions to distinguish between client-side and server-side errors from OpenCage API would help to react to HTTP errors.
Error code can be found at https://opencagedata.com/api#codes