use of cn.taketoday.web.bind.RequestBindingException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method controllerAdvice.
@Test
public void controllerAdvice() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
ctx.refresh();
ExceptionHandlerAnnotationExceptionHandler resolver = new ExceptionHandlerAnnotationExceptionHandler();
resolver.setApplicationContext(ctx);
resolver.afterPropertiesSet();
RequestBindingException ex = new RequestBindingException("message");
assertThat(resolver.handleException(request, ex, null)).isNotNull();
assertThat(this.servletResponse.getStatus()).isEqualTo(400);
assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
use of cn.taketoday.web.bind.RequestBindingException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedException.
@Test
public void controllerAdviceWithNestedException() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
ctx.refresh();
ExceptionHandlerAnnotationExceptionHandler resolver = new ExceptionHandlerAnnotationExceptionHandler();
resolver.setApplicationContext(ctx);
resolver.afterPropertiesSet();
IllegalStateException ex = new IllegalStateException(new RequestBindingException("message"));
assertThat(resolver.handleException(new ServletRequestContext(null, this.servletRequest, this.servletResponse), ex, null)).isNull();
}
use of cn.taketoday.web.bind.RequestBindingException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method servletRequestBindingException.
@Test
public void servletRequestBindingException() {
Exception ex = new RequestBindingException("message");
testException(ex);
}
use of cn.taketoday.web.bind.RequestBindingException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedExceptionWithinDispatcherServlet.
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
ctx.refresh();
DispatcherServlet servlet = new DispatcherServlet(ctx);
servlet.init(new MockServletConfig());
try {
servlet.service(this.servletRequest, this.servletResponse);
} catch (ServletException ex) {
boolean condition1 = ex.getCause() instanceof IllegalStateException;
assertThat(condition1).isTrue();
boolean condition = ex.getCause().getCause() instanceof RequestBindingException;
assertThat(condition).isTrue();
}
}
use of cn.taketoday.web.bind.RequestBindingException in project today-framework by TAKETODAY.
the class MatrixParamParameterResolvingStrategy method resolveName.
@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext request) throws Exception {
Map<String, MultiValueMap<String, String>> pathParameters = request.getMatchingMetadata().getMatrixVariables();
if (CollectionUtils.isEmpty(pathParameters)) {
return null;
}
MethodParameter parameter = resolvable.getParameter();
MatrixParam ann = parameter.getParameterAnnotation(MatrixParam.class);
Assert.state(ann != null, "No MatrixVariable annotation");
String pathVar = ann.pathVar();
List<String> paramValues = null;
if (!pathVar.equals(Constant.DEFAULT_NONE)) {
if (pathParameters.containsKey(pathVar)) {
paramValues = pathParameters.get(pathVar).get(name);
}
} else {
boolean found = false;
paramValues = new ArrayList<>();
for (MultiValueMap<String, String> params : pathParameters.values()) {
if (params.containsKey(name)) {
if (found) {
String paramType = parameter.getNestedParameterType().getName();
throw new RequestBindingException("Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
}
paramValues.addAll(params.get(name));
found = true;
}
}
}
if (CollectionUtils.isEmpty(paramValues)) {
return null;
} else if (paramValues.size() == 1) {
return paramValues.get(0);
} else {
return paramValues;
}
}
Aggregations