cancel
Showing results for 
Search instead for 
Did you mean: 

Handle BusinessException in activiti process

rafaelduqueestr
Champ in-the-making
Champ in-the-making
I'm searching for good solutions about to handle business exceptions inside of activiti process.

I noticed that, any exception throws inside the process execution is encapsulated as ActivitiException.

For example:

1- Process definition:


    <process id="processParametro">
        <startEvent id="iniciarProcessoParametro"/>
        <sequenceFlow sourceRef="iniciarProcessoParametro" targetRef="criarParametro"/>
        <serviceTask id="criarParametro"
                     name="Incluir Parametro"
                     activiti:expression="${parametroService.create(nomeParametro, descricaoParametro)}"
                     activiti:resultVariable="parametroCriado"/>
        <sequenceFlow sourceRef="criarParametro" targetRef="finalizarProcessoParametro"/>
        <endEvent id="finalizarProcessoParametro"/>
    </process>


2- The method create inside ParametroService class:


    @Transactional(readOnly = false)
    public Parametro create(String nome, String descricao) {
        Parametro parametro = parametroRepository.findByNome(nome);
        if (parametro != null){
            throw new BusinessException("domain.parametro.business.exception.nome.duplicado");
        }
        Parametro parametro = new Parametro(nome, descricao, SituacaoParametroEnum.PENDENTE_ATIVACAO.asDomain());
        return parametroRepository.save(parametro);
    }


If the method parametroRepository.findByNome(nome) return a parametro = null, the BusinessException will be thrown. But, the exception I'm receving in my client is ActivitiException.
I'm thinking to get a BusinessException inside the ActivitiException, but I think it isn't  a good design.

Someone has a better solution?
3 REPLIES 3

rafaelduqueestr
Champ in-the-making
Champ in-the-making
I've been trying to found a good solution for this question since friday.

I'm throwing a exception inside my business code and this business code is been used inside my process instance, but the Exception that arrived in my controller is ActivitiException.
There is in the ActivitiException a cause, but this cause isn't my BusinessException, this cause is ELException with this string: "api.business.exception.BusinessException: domain.parametro.business.exception.nome.duplicado".
I need this message: "domain.parametro.business.exception.nome.duplicado". I believe that there is a much better solution than catch a ActivitiException, get the ELException and parse the String "api.business.exception.BusinessException: domain.parametro.business.exception.nome.duplicado" to recovery the string "domain.parametro.business.exception.nome.duplicado".

Someone has a better solution?

rafaelduqueestr
Champ in-the-making
Champ in-the-making
Well, i wrote a ugly code to do what i want inside my @ControllerAdvice:

<java>
    @ExceptionHandler(ActivitiException.class)
    @ResponseBody
    public Map<String, Object> handleActivitiException(ActivitiException ex, HttpServletResponse response) {
        Throwable cause = ex.getCause();
        String[] messages = StringUtils.split(cause.getMessage(), ": ");
        String exceptionType = messages[0];
        String keyBundleException = messages[1];
        Class exceptionClass;
        try {
            exceptionClass = ClassUtils.forName(exceptionType, null);
        } catch (ClassNotFoundException cnfex) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return handleUncaughtException(cnfex);
        }
        if (BusinessException.class == exceptionClass) {
            if (ClassUtils.hasConstructor(BusinessException.class, String.class)) {
                Constructor<BusinessException> constructor = ClassUtils.getConstructorIfAvailable(BusinessException.class, String.class);
                BusinessException businessException = BeanUtils.instantiateClass(constructor, keyBundleException);
                response.setStatus(HttpServletResponse.SC_CONFLICT);
                return handleBusinessException(businessException);
            }
        }

        return handleUncaughtException(ex);
    }
</java>

If any activiti developer team has a better idea, please, send me.

saoussen
Champ in-the-making
Champ in-the-making

Hello, I'm trying to find a solution to the same problem.

somebody has an idea?

Thanks!