ATG Oracle Commerce 11.3 JAX-RS Handling Exceptions – RestException
ATG JAX-RS REST Endpoint resource exceptions are
handled as a RestException. RestException objects instance be
created using utility methods in RestUtils. This allows endpoint to
specify a response status code with an error .
The RestExceptionMapper instance
of the Jersey ExceptionMapper builds a response from the RestException that
is returned to the client. Exceptions are automatically rendered by the
framework output producer into the correct format before returning back to the
client.
When we build a RestException, we need to set HTTP
response codes, error messages, debug information and, optionally, error codes
that are returned from your endpoint method.
To understand the custom error message setting we will take
the below OOTB Resource
atg.commerce.order.restresources.CartRestResource
cancelCart() method will throw Internal server error when
current order is null.
RestException object created by providing error messages and
status code. The error messages are loaded from bundle.
@DELETE
@Endpoint(id = "/cart/DELETE", isSingular = true, filterId = "cart-Default")
@ApiOperation("Delete the current order from
the cart and replace with an empty order.")
public RepresentationModel cancelCart() throws RestException,
CommerceException, ServletException {
OrderHolder shoppingCart = this.getShoppingCart();
Order currentOrder = shoppingCart.getCurrent();
if (null == currentOrder) {
String executor1=ResourceUtils.getUserMsgResource("nullOrderHolder",
"atg.commerce.order.OrderResources",sResourceBundle);
throw new RestException(Status.INTERNAL_SERVER_ERROR.getStatusCode(),executor1);
} else {
if (this.isLoggingDebug()) {
this.logDebug("Initiating
cancelCart for : " + currentOrder.getId());
}
.......
return this.getRepresentationModelBuilder(shoppingCart.getCurrent()).build();
}
}
Error Response from postman app.
If we need to customize our error response then we need to
customize the component /atg/dynamo/service/jaxrs/RestExceptionMapper ‘s method
//atg.service.jaxrs.RestExceptionMapper
public Response
toResponse(RestException pException) {
return
JSONErrorResponseBuilder.buildResponse(pException);
}
No comments
Post a Comment