Error Handling on Java SDK for REST API service

后端 未结 5 1417
小鲜肉
小鲜肉 2021-02-04 01:37

We are building a Java SDK to simplify the access to one of our services that provide a REST API. This SDK is to be used by 3rd-party developers. I am struggling to find the bes

5条回答
  •  半阙折子戏
    2021-02-04 02:08

    It all comes down to how informative your API error responses are. The more informative the error handling of the API is, the more informative the exception handling can be. I would believe the exceptions would only be as informative as the errors returned from the API.

    Example:

    { "status":404,"code":2001,"message":"Photo could not be found."}
    

    Following your first suggestion, if the Exception contained both the status and the API error code, the developer has a better understanding of what he needs to do and more option when it comes to exception handling. If the exception also contained the error message that was returned, as well, the developer shouldn't even need to reference the documentation.

提交回复
热议问题