Error Handling on Java SDK for REST API service

后端 未结 5 1336
小鲜肉
小鲜肉 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:23

    I've seen libraries that combine your suggestions 2 and 3, e.g.

    public Photo getPhoto(String photoID) throws RestServiceException, UnauthenticatedException, UnauthorizedException, NotFoundException;
    

    This way, when you add a new checked exception that extends RestServiceException, you're not changing the method's contract and any code using it still compiles.

    Compared to a callback or unchecked exception solution, an advantage is that this ensures your new error will be handled by the client code, even if it's only as a general error. In a callback, nothing would happen, and with an unchecked exception, your client application might crash.

提交回复
热议问题