I have a service A that makes post request to another controller (B). This is my service making post request. Controller B is not in the same project as service A, so B throws Bad request (400) and service A turns 400 request to WebApplicationException:
WebClient client = tokenAuth.addAuthentication(WebClient.create(url))
.type(AccelaradMediaType.SMR_IMAGE_SHARE_V3_JSON)
.accept(AccelaradMediaType.SMR_SHARE_RESULT_JSON);
String response = client.post(body, String.class);
catch (WebApplicationException e) {
//get message from exception and print
}
And this is other controller(B) that my service is making post request:
#POST
#Path("/shares")
#Consumes({AccelaradMediaType.SMR_IMAGE_SHARE_V3_JSON, AccelaradMediaType.SMR_IMAGE_SHARE_V3_XML})
#Produces({AccelaradMediaType.SMR_SHARE_RESULT_JSON, AccelaradMediaType.SMR_SHARE_RESULT_XML})
public ShareResult shareV3() {
ShareResult result = null;
try {
result = shareStudies();
}
catch (StudyShareException e) {
LOG.error(e.getMessage(), e);
throw new BadRequestException(e.getMessage());
}
return result;
}
public ShareResult shareStudies() {
try {
//some logic
}
catch (InvitationException e) {
String message = "Invitation is pending";
throw new StudyShareException(message, e);
}
}
And here are StudyShareException class and BadRequestException class:
public class StudyShareException extends Exception {
public StudyShareException(String message, Throwable cause) {
super(message, cause);
}
}
public class BadRequestException extends WebApplicationException {
private static final long serialVersionUID = 1L;
public BadRequestException(String message) {
this(message, null);
}
public BadRequestException(String message, Throwable cause) {
super(cause, Response.status(Response.Status.BAD_REQUEST).entity(message).type(MediaType.TEXT_PLAIN).build());
}
}
When service A makes a post request, it does go into the catch block and controller B prints out error in the stack trace with "Invitation is pending" message.
My goal is to print out "Invitation is pending" from service A as well. I tried e.getResponse() or e.getResponse().getEntity() or e.getMessage(), nothing has worked. Is it even possible to get custom message from service A? If so, how can I achieve this?
Why are you catching WebApplicationException in service A when service B is throwing StudyShareException? You need to catch the correct exception in service A
try {
WebClient client = tokenAuth.addAuthentication(WebClient.create(url))
.type(AccelaradMediaType.SMR_IMAGE_SHARE_V3_JSON)
.accept(AccelaradMediaType.SMR_SHARE_RESULT_JSON);
String response = client.post(body, String.class);
catch (StudyShareException e) {
//get message from exception and print
}
Now if you are trying to catch all exceptions extending WebApplicationException then you should make StudyShareException extend WebApplicationException as well.
That being said, perhaps you shouldn't catch any exception at all just let service A just bubble up the exception thrown in service B. However, that's up to you, you may want to throw a different message or a different exception in service A.
Related
I have a controller advice that handles the exceptional behavior in my REST controller and I came across a situation when I have to conditionally process SQLIntegrityConstraintViolationException that have a certain message (the one for duplicate keys), returning a 409, letting the other ones be handled by the default handler (returning a 500 error code).
I am thinking of 2 possible ways to achieve that:
Throwing a new bare-boned Exception on the else branch on my condition, so the handling is done by Spring.
Explicitly calling the general exception handler (like return handleGeneralException(exception) from inside my else branch).
I there a "proper" way to pass on a fraction of exceptions of one kind in my ControllerAdvice to another handler than the "original" one?
EDIT 1:
I would like to do something like this in my ControllerAdvice:
if (exception.getMessage.contains("something")) {
// handle exception
} else {
// pass to other handler
}
Have a custom exception class and then when you throw the SQLIntegrityConstraintViolationException wrap it in your custom exception classs with additional fields whatever you want to be accessible in controller advice. Handle the custom exception in your controller advice class.
#ControllerAdvice
public class CustomExceptionHandler {
#ExceptionHandler(YourCustomException.class)
public final ResponseEntity<ExceptionResponse> handleNotFoundException(YourCustomExceptionex,
WebRequest request) {
ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(),
request.getDescription(false), HttpStatus.NOT_ACCEPTABLE.getReasonPhrase());
return new ResponseEntity<>(exceptionResponse, HttpStatus.CONFLICT);
}
}
While having the try catch block to handle this exception in your code , make sure that you handle DataIntegrityViolationException instead of SQLIntegrityConstraintViolationException if you are using Spring Data JPA. So , if you are using Spring Data Jpa then :
try {
anyRepository.save(new YourModel(..));
} catch (DataIntegrityViolationException e) {
System.out.println("history already exist");in res
throw New YourCustomException("additional msg if you need it ", e);
}
Below code will capture the error message of exception SQLIntegrityConstraintViolationException in ControllerAdbvice without having to handle in code
#ControllerAdvice
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler(value = DataIntegrityViolationException.class)
public ResponseEntity<ExceptionResponse> dataIntegrityViolationExceptionHandler(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
Throwable throwable = ex.getCause();
while (throwable != null) {
if (throwable instanceof SQLIntegrityConstraintViolationException) {
String errorMessage = throwable.getMessage();
response.setErrors(new ArrayList<>(Arrays.asList(errorMessage)));
}
throwable = throwable.getCause();
}
return new ResponseEntity<Object>(response, HttpStatus.CONFLICT);
}
}
I am creating soap web service using spring. I am getting hibernate exception while trying to save the request.
I am trying to catch the hibernate exception in the catch block but control not even coming to the catch block
and soap service returning with the soap fault error message. Below are the list of classes which i am using,
Could any one please let me know how to handle the exception and rethrow the exception.
#WebService(serviceName = "submitService")
public class SubmitService extends AbstractWebServiceImpl {
#Autowired
private Validate validate;
#WebMethod
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public ResponseData submitRequest(RequestData request) {
ResponseData response = validate.submitRequest(request);
return response;
}
}
My Base class
public class AbstractWebServiceImpl extends SpringBeanAutowiringSupport {
#Resource
private WebServiceContext webServiceContext;
protected void handleWSException(Exception e) throws BusinessFault, InfrastructureFault {
if ( e instanceof BusinessException) {
ReturnMessage rm = ((BusinessException)e).getReturnMessage();
throw new BusinessFault(rm, e);
} else if (e instanceof BaseException) {
ReturnMessage rm = ((BaseException)e).getReturnMessage();
throw new InfrastructureFault(rm, e);
} else {
ReturnMessage rm = new ReturnMessage(ReturnCode.GENERIC_WEB_SERVICE_ERROR, e.toString());
throw new InfrastructureFault(rm, e);
}
}
public void setWebServiceContext(WebServiceContext webServiceContext) {
this.webServiceContext = webServiceContext;
}
public WebServiceContext getWebServiceContext() {
return webServiceContext;
}
}
My Business layer implementation class
#Component
public class ValidateImpl implements Validate {
#Autowired
private SomeHibernateDao dao;
#Override
#Transactional
public ResponseData submitRequest(RequestData request) {
ResponseData response = new ResponseData();
try {
dao.save(request);
} catch (Exception e) {
// Control never execute this block of code if dao layer throwing any exception.
// I want to catch the exception here modify the response and return to the client
e.printStackTrace();
response.setErrorDetails("More meaningful error message");
}
return response;
}
This code returning default soap error message back to client.
I want to catch the exception and modify the exception before returning to client. Please let me know what change i have to make so that i can handle the soap error message before i return the response back to client.
I am able to catch the exception in SubmitService itself but not sure why not able to catch the exception in ValidateImpl. however my issue is resolved now.
I am new to Spring Boot, and I am trying to test a connection using HTTP OPTIONS.
My design is that I have a Service class that contains the logics for the testing. I also have an API Controller class that implements the method from Service.
My currently understanding is that the controller can be used to respond back different HTTP statuses using exceptions.
This is the method I wrote inside the controller for this purpose:
#PostMapping(path = "/test")
public ResponseEntity<Void> testConnection(#RequestBody URL url) {
try {
ControllerService.testConnection(url);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
} catch (CredentialsException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(null);
} catch (URLException | URISyntaxException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
} catch (UnknownException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
The way exceptions are triggered and the method testConnection() are inside the service class:
public static void testConnection(URL url)
throws URISyntaxException, CredentialsException, URLException, UnknownException {
String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(("user" + ':' + "password").getBytes());
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Authorization", authHeaderValue);
RestTemplate rest = new RestTemplate();
final ResponseEntity<Object> optionsResponse = rest.exchange(url.toURI(), HttpMethod.OPTIONS, new HttpEntity<>(requestHeaders), Object.class);
int code = optionsResponse.getStatusCodeValue();
if (code == 403) {
throw new InvalidCredentialsException();
} else if (code == 404) {
throw new InvalidURLException();
} else if (code == 500) {
throw new UnknownErrorException();
} else if (code == 200){
String message = "Test connection successful";
LOGGER.info(message);
}
}
I have created those custom exception classes.
Is this the proper way to trigger the right HTTP response inside the controller method or does Spring Boot has some other design? If so, is my list of exceptions comprehensive enough or do I need to add more to the testConnection() method in the service class?
You can write ExceptionHandler for each of the Exception type, so you don't have to repeat the code or use try/ catch block at all. Just let your testConnection and other methods to throw the exception.
import org.springframework.web.bind.annotation.ExceptionHandler;
#ExceptionHandler(CredentialsException.class)
public void credentialsExceptionHandler(CredentialsException e, HttpServletResponse response) throws IOException {
response.sendError(HttpStatus.FORBIDDEN.value(), e.getMessage());
}
There are different ways to define and use the ExceptionHandler method. But conceptually same.
I've a flow where on CXF client I've jaxrs-in-interceptor, provider and exception mapper. In my case I'm catching bad response from client through in-interceptor and then I would like abort the cxf bus chain and throw a fault. Unfortunately I couldn't do it, cause in every situation exception thrown from interceptor is being only logged, but the main error (wrong json format) is propagated to exception mapper. I would like to avoid Exception mapper, but I don't know how. I'm using WebClient to implement interceptors like this:
#Component
public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
public MyInterceptor() {
super(POST_STREAM);
}
#Override
public void handleMessage(Message message) throws Fault {
if (message != null) {
//message.getExchange().setOneWay(true);
//message.getExchange().put(Exception.class, new MyException());
//message.getInterceptorChain().abort();
//message.setContent(Exception.class, new MyException());
//Endpoint ep = message.getExchange().get(Endpoint.class);
//message.getInterceptorChain().abort();
//if (ep.getInFaultObserver() != null) {
// ep.getInFaultObserver().onMessage(message);
//}
//throw new WebApplicationException( new MyException());
//message.setContent(Response.class, response);
throw new Fault(new MyException());
}
}
I read that I should implement jaxrs-filter cause exceptions thrown by interceptor are not propagated to exception mapper. Is it any way to do that in java thanks to WebClient implementation?
S client = create(url, clazz, list(jsonProvider(), providers));
WebClient.getConfig(client).getInInterceptors().add(new MyInterceptor());
I've also tried to use different phases on interceptor, but it also didn't work.
I have been researching and testing with your issue. The problem is that the exceptions thrown from the CXF interceptors escape the JAX-RS flow (see the answer of CXF team)
A Fault generated from interceptor can be catched implementing handleFault in the interceptor itself
public void handleFault(Message message) {
Exception e = message.getContent(Exception.class);
}
Or implementing a FaultListener and registering it at CXF Bus
WebClient.getConfig(client).getBus().getProperties().put("org.apache.cxf.logging.FaultListener",new MyFaultListener());
public class MyFaultListener implements FaultListener{
public boolean faultOccurred(final Exception exception,final String description,final Message message) {
//return false to avoid warning of default CXF logging interceptor
return false;
}
}
But you can not return custom response from interceptor or respond a Fault to client.
The workaround I have found to achieve the desired behaviour consist in replacing the Response with a custom object that could be processed by your usual method invokation, like an exceptionMapper
See CXF/ JAX-RS : Return Custom response from interceptor
Into Interceptor.handleMessage check the conditions you need and create a Response with custom status and entity. After this, stop the chain
public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
public MyInterceptor() {
super(Phase.POST_STREAM);
}
#Override
public void handleMessage(Message message) throws Fault {
if (message != null) {
//check the condition to raise the error
//build the custom Response replacing service call
Response response = Response
.status(Response.Status.BAD_REQUEST)
.entity("custom error")
.build();
message.getExchange().put(Response.class, response);
//abort interceptor chain in you want to stop processing or throw a Fault (catched by handleFault)
//message.getInterceptorChain().abort();
//throw new Fault (new MyException());
}
public void handleFault(Message messageParam) {
}
}
Add the ResponseExceptionMapper as provider when creating the JAXRS client
providers.add(new ResponseExceptionMapper<WebApplicationException>() {
#Override
public WebApplicationException fromResponse(Response r) {
return new WebApplicationException(r);
}
});
YourService proxy = JAXRSClientFactory.create(url, clazz,providers);
Client client = WebClient.client(proxy);
WebClient.getConfig(client).getInInterceptors().add(new MyInterceptor());
After this, a call to proxy.yourService() will raise a WebApplicationException if acomplish the interceptor check. You can catch it or rethrow in the desired way
try{
proxy.yourService();
}catch (WebApplicationException e){
}
Hope this helps
I fully agree with previous answer. My implementation looks like:
#Component
public class ServiceFailureInterceptor extends AbstractPhaseInterceptor<Message> {
private static final Logger LOG = LoggerFactory.getLogger(ServiceFailureInterceptor.class);
public ServiceFailureInterceptor() {
super(PRE_STREAM);
}
#Override
public void handleMessage(Message message) {
if (message != null) {
int responseCode = (int) message.get(Message.RESPONSE_CODE);
LogicException logicException = ErrorMapper.HTTP_STATUS_CODE_MAPPER.get(responseCode);
InputStream is = b2stream(MapperUtils.json().toBytes(logicException));
// clear old message & exchange
Exchange exchange = message.getExchange();
for (Class<?> contentFormat : message.getContentFormats()) {
message.setContent(contentFormat, null);
}
resetOrigInterceptorChain(message);
resetFault(exchange);
message.setContent(InputStream.class, is);
Message outMessage = createOutMessage(exchange, is);
prepareMessage(outMessage);
prepareMessage(message);
}
}
private void prepareMessage(Message message) {
message.put(Message.REQUESTOR_ROLE, true);
message.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
}
private Message createOutMessage(Exchange exchange, InputStream logicException) {
Endpoint ep = exchange.get(Endpoint.class);
Message outMessage = ep != null ? ep.getBinding().createMessage() : new MessageImpl();
outMessage.setContent(InputStream.class, logicException);
exchange.setOutMessage(outMessage);
outMessage.setExchange(exchange);
return outMessage;
}
private void resetFault(Exchange exchange) {
exchange.put(Exception.class, null);
}
private void resetOrigInterceptorChain(Message message) {
InterceptorChain chain = message.getInterceptorChain();
if (chain != null) {
for (Interceptor<?> interceptor : chain) {
chain.remove(interceptor);
}
chain.reset();
}
}
}
After setting this exception manually I'm going to ExceptionMapper implementation where my LogicException is consumed and response with exception is building. I cannot avoid Exception mapper when is declared as a provider through WebClient, so I've decided to use it and remapped Exception later.
I'm having trouble handling exceptions in my RESTful service:
#Path("/blah")
#Stateless
public class BlahResource {
#EJB BlahService blahService;
#GET
public Response getBlah() {
try {
Blah blah = blahService.getBlah();
SomeUtil.doSomething();
return blah;
} catch (Exception e) {
throw new RestException(e.getMessage(), "unknown reason", Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
RestException is a mapped exception:
public class RestException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String reason;
private Status status;
public RestException(String message, String reason, Status status) {
super(message);
this.reason = reason;
this.status = status;
}
}
And here is the exception mapper for RestException:
#Provider
public class RestExceptionMapper implements ExceptionMapper<RestException> {
public Response toResponse(RestException e) {
return Response.status(e.getStatus())
.entity(getExceptionString(e.getMessage(), e.getReason()))
.type("application/json")
.build();
}
public String getExceptionString(String message, String reason) {
JSONObject json = new JSONObject();
try {
json.put("error", message);
json.put("reason", reason);
} catch (JSONException je) {}
return json.toString();
}
}
Now, it is important for me to provide both a response code AND some response text to the end user. However, when a RestException is thrown, this causes an EJBException (with message "EJB threw an unexpected (non-declared) exception...") to be thrown as well, and the servlet only returns the response code to the client (and not the response text that I set in RestException).
This works flawlessly when my RESTful resource isn't an EJB... any ideas? I've been working on this for hours and I'm all out of ideas.
Thanks!
The problem seems to be connected with EJB Exception Handling. By the specification any system exception (that is - any RuntimeException not specifically marked as Application Exception) that is thrown from within a managed bean will be packaged into an EJBException and later, if needed, into RemoteException thrown to the client. That is a situation you seem to be in, and in order to avoid that you can either:
change your RestException into a checked exception and handle it as such
use #ApplicationException annotation on your RestException
create EJBExceptionMapper and extract information needed from (RestfulException) e.getCause()
A similar case works for me when RestException extends javax.ws.rs.WebApplicationException