@RestControllerAdvice
public class InotApiControllerAdvice {
...
@ExceptionHandler(UpdateAssetAuthenticationException.class)
public Mono<ResponseEntity<JobResponse>> handleUpdateAssetAuthenticationException(UpdateAssetAuthenticationException e) {
if(e.getNbRetry() >= MAX_RETRIES) {
return Mono.error(new Exception("Authentication Exception in UpdateAsset for id "+e.getId()+" - name "+e.getName()+" - value "+e.getValue()));
}
LOG.info("Authentication Exception in UpdateAsset for id {} - name {} - value {}... Retrying with new token", e.getId(), e.getName(), e.getValue());
Mono<String> refreshToken = inotApiService.doRefreshToken();
if(refreshToken != null) {
return refreshToken.doOnSuccess(token -> {
inotApiService.setAccessToken(token);
}).then(inotApiService.createAsset(e.getName(), e.getValue(), e.getNbRetry() + 1))
.then(inotApiService.startJob(e.getName()))
.flatMap(response -> just(ResponseEntity.ok(response)));
}
return Mono.error(new Exception("No valid authentication method found in UpdateAsset for id "+e.getId()+" - name "+e.getName()+" - value "+e.getValue()));
}
}
调用 inotApiService.startJob 时可能会引发异常,其处理方法如下:
@ExceptionHandler(InotApiException.class)
public ResponseEntity<ErrorResponse> handleInotApiException(InotApiException iae) {
return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8)
.body(new ErrorResponse(iae.getMessage()));
}
问题是当引发异常时,不会调用此处理程序方法,并且 AbstractErrorWebExceptionHandler
会得到500响应。任何关于即使在 @RestControllerAdvice 类中抛出该异常也如何处理的想法?