I have two different Aspect Classes, aspect class 1 would take care of any exceptions that would occur in all of my controller files and the other, aspect class 2, would handle few specific exceptions that would occur only in one specific controller file. I have specified the precedence order, giving aspect class 2 higher precedence over aspect1. Aspect class 1 is as below
@Aspect
@Order(1)
public class TrackOperations {
@Around("apiPointcut()")
public Object handleException(ProceedingJoinPoint pjp) throws Throwable {
try {
return pjp.proceed();
} catch (Exception e) {
LOGGER.error("Caught exception: ", e);
StringBuilder sb = getExceptionTrace(e);
StringWriter errorStackTrace = new StringWriter();
e.printStackTrace(new PrintWriter(errorStackTrace, true));
response.addError(error);
return new ResponseEntity<String>(JsonUtil.toJson(response), HttpStatus.INTERNAL_SERVER_ERROR);
}}
Aspect class 2
@Aspect
@Order(0)
public class TrackServiceOperations {
@Around("apiPointcut()")
public Object handleServerException(
try {
return pjp.proceed();
} catch (HttpClientErrorException | HttpServerErrorException e) {
response = (Response<String>) ContextUtil.get(key);
response.addError(new Error("Assessment Service Status Code: " + e.getStatusCode(), ErrorConstants.REQUEST_FAILED));
LOGGER.error("Recieved" + e.getStatusCode() + " status from Assessment Service");
return new ResponseEntity<String>(JsonUtil.toJson(response), HttpStatus.OK);
}
}}
Have mentioned the same in the application xml
<bean id="TrackServiceOperations" class="in.foo.foo.TrackServiceOperations" factory-method="aspectOf" />
<bean id="TrackOperations" class="in.foo.foo.TrackOperations" factory-method="aspectOf" />
But when the controller file throws the desired exception, aspect class 1 ends up handling the exception instead of aspect class 2. When I disable aspect class 1 things work out fine as expected, but when I use both the @around pointcuts, the precedence order doesn't seem to work. Am I missing something here?