@Override public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) { HandlerMethod handlerMethod = (HandlerMethod) handler; InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod); invocable.setHandlerMethodArgumentResolvers(this.argumentResolvers); ModelMap model = new ExtendedModelMap(); return invocable .invokeForRequest(exchange, model) .map( result -> result.setExceptionHandler(ex -> handleException(ex, handlerMethod, exchange))) .otherwise(ex -> handleException(ex, handlerMethod, exchange)); }
private Mono<HandlerResult> handleException( Throwable ex, HandlerMethod handlerMethod, ServerWebExchange exchange) { if (ex instanceof Exception) { InvocableHandlerMethod invocable = findExceptionHandler(handlerMethod, (Exception) ex); if (invocable != null) { try { if (logger.isDebugEnabled()) { logger.debug("Invoking @ExceptionHandler method: " + invocable); } invocable.setHandlerMethodArgumentResolvers(getArgumentResolvers()); ExtendedModelMap errorModel = new ExtendedModelMap(); return invocable.invokeForRequest(exchange, errorModel, ex); } catch (Exception invocationEx) { if (logger.isErrorEnabled()) { logger.error("Failed to invoke @ExceptionHandler method: " + invocable, invocationEx); } } } } return Mono.error(ex); }