public final class ExceptionDocumentation extends Object
Open the exception hierarchy image in a new tab.
The JVM SDK makes use of exceptions of the Java JDK, such asIllegalArgumentException
, and provides own exceptions which all inherit from SphereException
.
Problems concerning the HttpClient
throw a HttpException
.
JSON serializing and deserializing problems throw JsonException
.
SphereServiceException
is a base exception for all error responses from the commercetools Composable Commerce APIs (HTTP status code >= 400
).
ClientErrorException
expresses errors which can be recovered by the client side (HTTP status code >= 400 and < 500
).
ServerErrorException
is for server errors.
ErrorResponseException
into a CompletionStage
.
The following example shows how to distinguish errors by error code:
final String wrongPassword = "wrong password";
final CustomerSignInCommand signInCommand = CustomerSignInCommand.of(email, wrongPassword);
final CompletionStage<CustomerSignInResult> future = client.execute(signInCommand);
future.whenCompleteAsync((signInResult, exception) -> {
if (signInResult != null) {
println("Signing worked");
} else if (exception instanceof ErrorResponseException) {
final ErrorResponseException errorResponseException = (ErrorResponseException) exception;
final String code = CustomerInvalidCredentials.CODE;
if (errorResponseException.hasErrorCode(code)) {
println("customer has invalid credentials");
}
}
});
See the test code.
InvalidJsonInputError
hinting to the problem with the JSON:
final String json = "{\n" +
" \"statusCode\" : 400,\n" +
" \"message\" : \"Request body does not contain valid JSON.\",\n" +
" \"errors\" : [ {\n" +
" \"code\" : \"InvalidJsonInput\",\n" +
" \"message\" : \"Request body does not contain valid JSON.\",\n" +
" \"detailedErrorMessage\" : \"detailed error message\"" +
" } ]\n" +
"}";
final ErrorResponse sphereErrorResponse = SphereJsonUtils.readObject(json, ErrorResponse.typeReference());
final InvalidJsonInputError jsonError = sphereErrorResponse.getErrors().get(0).as(InvalidJsonInputError.class);
assertThat(jsonError.getDetailedErrorMessage()).isEqualTo("detailed error message");
See the test code.
SphereException