cancel
Showing results for 
Search instead for 
Did you mean: 

Exception Handling in java service task

prash_aggarwal
Champ in-the-making
Champ in-the-making
Hi,

I have a java service task  to check the file type validity, i perform this check and want to throw an exception with message like
"Uploaded file type application/pd not supported"

       if(!fileTypeValid)
          throw new ABCException("Uploaded file type " + fileType +" not supported");

The stack trace i get is
org.activiti.engine.ActivitiException: Error while evaluating expression: ${uploadService.fileTypeValidityCheck(execution)}

Caused by: org.activiti.engine.impl.javax.el.ELException: com.markit.aft.exception.MIMException: Uploaded file type application/pd not supported

Caused by: com.xyz.aft.exception.ABCException: Uploaded file type application/pd not supported

How do i show the original message thrown in the exception ie "Uploaded file type " + fileType +" not supported"

Currently I am doing error.setMessage(e.getCause().getCause().getMessage()); as shown below, is this the right way?
Please guide..

@ExceptionHandler({ActivitiException.class})
    public ResponseEntity<ErrorResponse> handleActivitiException(ActivitiException e){
      ErrorResponse error = new ErrorResponse();
      error.setErrorCode(HttpStatus.BAD_REQUEST.value());
      error.setMessage(e.getCause().getCause().getMessage());
      String errorId = UUID.randomUUID().toString();
      error.setErrorId(errorId);
      LOG.error(error.toString());
      return new ResponseEntity<ErrorResponse>(error, HttpStatus.BAD_REQUEST);
       
    }

Thanks

2 REPLIES 2

prash_aggarwal
Champ in-the-making
Champ in-the-making
Is this a good way to return the actual message i wanted to return to webapp?
@ExceptionHandler({ActivitiException.class})
    public ResponseEntity<ErrorResponse> handleActivitiException(ActivitiException e){
  ErrorResponse error = new ErrorResponse();
  error.setErrorCode(HttpStatus.BAD_REQUEST.value());
  Throwable realCause = findTheRealCause(e);
  error.setMessage(realCause.getMessage());
  String errorId = UUID.randomUUID().toString();
  error.setErrorId(errorId);
  LOG.error(error.toString());
  return new ResponseEntity<ErrorResponse>(error, HttpStatus.BAD_REQUEST);
       
    }

jbarrez
Star Contributor
Star Contributor
Yes - if the exception happens in resolving the expression, i think it's the only way.
An alternative could be to catch the exception in the delegate, throw a BpmnError and handle it explicitely in the BPMN process.