this.initBinderAdviceCache.put(bean, binderMethods); ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType); if (resolver.hasExceptionMappings()) { this.exceptionHandlerAdviceCache.put(bean, resolver);
@Test(expected = IllegalStateException.class) public void ambiguousExceptionMapping() { new ExceptionHandlerMethodResolver(AmbiguousController.class); }
@Test(expected = IllegalStateException.class) public void noExceptionMapping() { new ExceptionHandlerMethodResolver(NoExceptionController.class); }
throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean); ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType); if (resolver.hasExceptionMappings()) { this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
@Test public void resolveMethodFromAnnotation() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); IOException exception = new IOException(); assertEquals("handleIOException", resolver.resolveMethod(exception).getName()); }
@Test public void resolveMethodFromArgument() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); IllegalArgumentException exception = new IllegalArgumentException(); assertEquals("handleIllegalArgumentException", resolver.resolveMethod(exception).getName()); }
@Test public void resolveMethodBestMatch() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); SocketException exception = new SocketException(); assertEquals("handleSocketException", resolver.resolveMethod(exception).getName()); }
@Test public void resolveMethodInherited() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(InheritedController.class); IOException exception = new IOException(); assertEquals("handleIOException", resolver.resolveMethod(exception).getName()); }
ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(handlerType); if (resolver == null) { resolver = new ExceptionHandlerMethodResolver(handlerType); this.exceptionHandlerCache.put(handlerType, resolver);
throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean); ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType); if (resolver.hasExceptionMappings()) { this.exceptionHandlerAdviceCache.put(adviceBean, resolver);
@Test public void resolveMethodExceptionSubType() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); IOException ioException = new FileNotFoundException(); assertEquals("handleIOException", resolver.resolveMethod(ioException).getName()); SocketException bindException = new BindException(); assertEquals("handleSocketException", resolver.resolveMethod(bindException).getName()); }
@Test public void resolveMethodNoMatch() { ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class); Exception exception = new Exception(); assertNull("1st lookup", resolver.resolveMethod(exception)); assertNull("2nd lookup from cache", resolver.resolveMethod(exception)); }
ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(handlerType); if (resolver == null) { resolver = new ExceptionHandlerMethodResolver(handlerType); this.exceptionHandlerCache.put(handlerType, resolver);
private ExceptionHandlerExceptionResolver createExceptionResolver() { ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() { @Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod( HandlerMethod handlerMethod, Exception exception) { Method method = new ExceptionHandlerMethodResolver( MyAppExceptionHandler.class).resolveMethod(exception); return new ServletInvocableHandlerMethod( new MyAppExceptionHandler(), method); } }; exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); exceptionResolver.afterPropertiesSet(); return exceptionResolver; }
/** * Returns a new "exception handler method resolver" that knows how to resolve exception handler methods based on the "herd error information exception * handler". This provides the ability to return an "exception handling" method for a specific exception. * * @return the exception handler method resolver. */ @Bean public ExceptionHandlerMethodResolver exceptionHandlerMethodResolver() { return new ExceptionHandlerMethodResolver(HerdErrorInformationExceptionHandler.class); }
/** * Returns a new "exception handler method resolver" that knows how to resolve exception handler methods based on the "herd error information exception * handler". This provides the ability to return an "exception handling" method for a specific exception. * * @return the exception handler method resolver. */ @Bean public ExceptionHandlerMethodResolver exceptionHandlerMethodResolver() { return new ExceptionHandlerMethodResolver(HerdErrorInformationExceptionHandler.class); }
protected InvocableHandlerMethod findExceptionHandler(HandlerMethod handlerMethod, Throwable exception) { if (handlerMethod == null) { return null; } Class<?> handlerType = handlerMethod.getBeanType(); ExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(handlerType); if (resolver == null) { resolver = new ExceptionHandlerMethodResolver(handlerType); this.exceptionHandlerCache.put(handlerType, resolver); } Method method = resolver.resolveMethodByExceptionType(exception.getClass()); return (method != null ? new InvocableHandlerMethod(handlerMethod.getBean(), method) : null); }
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller) .setHandlerExceptionResolvers(withExceptionControllerAdvice()) .build(); private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() { final ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() { @Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod(final HandlerMethod handlerMethod, final Exception exception) { Method method = new ExceptionHandlerMethodResolver(TestAdvice.class).resolveMethod(exception); if (method != null) { return new ServletInvocableHandlerMethod(new TestAdvice(), method); } return super.getExceptionHandlerMethod(handlerMethod, exception); } }; exceptionResolver.afterPropertiesSet(); return exceptionResolver; }
@Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod(final HandlerMethod handlerMethod, final Exception exception) { if (((InvocationException) exception).getStatusCode() == HttpStatus.BAD_REQUEST.value()) { Method method = new ExceptionHandlerMethodResolver(BadRequestExceptionAdvice.class).resolveMethod(exception); return new ServletInvocableHandlerMethod(new BadRequestExceptionAdvice(), method); } else if (((InvocationException) exception).getStatusCode() == HttpStatus.TOO_MANY_REQUESTS.value()) { Method method = new ExceptionHandlerMethodResolver(TooManyRequestsExceptionAdvice.class).resolveMethod(exception); return new ServletInvocableHandlerMethod(new TooManyRequestsExceptionAdvice(), method); } return super.getExceptionHandlerMethod(handlerMethod, exception); } }
@Override protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) { Method method = new ExceptionHandlerMethodResolver(RestExceptionHandler.class).resolveMethod(exception); RestExceptionHandler validationHandler = new RestExceptionHandler(); validationHandler.setMessageSource(messageSource); return new ServletInvocableHandlerMethod(validationHandler, method); } };