cancel
Showing results for 
Search instead for 
Did you mean: 

How configure global exception handler in alfresco CE 5.1.f?

v_yaml
Champ in-the-making
Champ in-the-making

I trying configure global exception handler, but it doesn't work.

1-st variant:

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExHandler extends ResponseEntityExceptionHandler {

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    protected ResponseEntity<Object> handleConflict(Exception ex, WebRequest request) {
        String bodyOfResponse = "This should be application specific";
        return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
    }
}

2-nd variant:

@Component
public class ExResolver extends AbstractHandlerExceptionResolver {
    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }

    @Override
    protected ModelAndView doResolveException
            (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        try {
            if (ex instanceof IllegalArgumentException) {
                return handleIllegalArgument((IllegalArgumentException) ex, response);
            }
        } catch (Exception handlerException) {
            logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
        }
        return null;
    }

    private ModelAndView handleIllegalArgument(IllegalArgumentException ex, HttpServletResponse response) throws IOException {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return new ModelAndView();
    }
}

src/main/resources/alfresco/web-extension/custom-slingshot-application-context.xml

<?xml version='1.0' encoding='UTF-8'?>
<!-- This is a sample configuration file from Alfresco Community - Please adapt to your version -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="net.example.alfresco"/>
</beans>

Beans success created, but it not work

What am I missing or doing wrong?

1 REPLY 1

afaust
Legendary Innovator
Legendary Innovator

Alfresco does not use Spring Boot, just simple Spring beans. Nothing is looking for or using any exception handler or resolver, so defining one has no effect at all. In short, there is no support for adding generic / global exception handlers in Alfresco Content Services - exception handling is done as part of the individual ReST API operation code.