FeignClient によって接続された別のマイクロサービスから受け取った例外をキャッチしようとしています。カスタム ErrorDecoder を作成し、
public class CustomErrorDecoder implements ErrorDecoder {
private final Logger log = LoggerFactory.getLogger(getClass());
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
log.info("----------------- "+methodKey+" , "+response.status()+" , "+ response.reason());
return new RestApiException(99,99,"");
}
return defaultErrorDecoder.decode(methodKey, response);
}
}
RestApiException は Exception を拡張します。
@ControllerAdvice
public class GlobalControllerAdvice {
private final Logger log = LoggerFactory.getLogger(getClass());
@ExceptionHandler(RestApiException.class)
public ResponseEntity<RestApiException> handleException(RestApiException exception, HttpServletRequest req) {
log.error("Sending error to client ( "+req.getUserPrincipal().getName()+" ) \"{}\"", exception.getErrMsg());
return new ResponseEntity<RestApiException>(exception, exception.getStatus());
}
@ExceptionHandler(Throwable.class)
public ResponseEntity<RestApiException> handleException(Throwable throwable, HttpServletRequest req) {
RestApiException exception=new RestApiException(HttpStatus.INTERNAL_SERVER_ERROR, 100, 100,
throwable.getMessage());
return handleException(exception, req);
}
その結果、<--- HTTP/1.1 400 Bad Request (5380ms) を取得すると、デフォルトのエラー メッセージが表示されます
HttpStatus.INTERNAL_SERVER_ERROR, 100, 100, throwable.getMessage());
ただし、CustomErrorDecoder で設定しようとしているカスタム例外はありません。
私が間違っていること、なぜ RetAppiException を呼び出してエラー応答を残りのクライアントに返すことができないのか。
ありがとう。