Exception handling

Chapter 3, The First Endpoint, touched upon defining a consistent response format including error codes and error descriptions. The corollary on the client side is to define an exception that encapsulates information about why a particular call failed. We could, therefore, define the following exception:

public class ClientException extends RuntimeException { private final int errorCode; private final String errorDescription; public ClientException(ApiError error) { super(error.getErrorCode() + ": " + error.getDescription()); this.errorCode = error.getErrorCode(); this.errorDescription = error.getDescription(); } public int getErrorCode() { return errorCode; } public String getErrorDescription() { return errorDescription; } ...

Get Building a RESTful Web Service with Spring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.