Search in sources :

Example 1 with RequestBindingException

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");
}
Also used : RequestBindingException(cn.taketoday.web.bind.RequestBindingException) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) ExceptionHandlerAnnotationExceptionHandler(cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler) Test(org.junit.jupiter.api.Test)

Example 2 with RequestBindingException

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();
}
Also used : RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) ExceptionHandlerAnnotationExceptionHandler(cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler) Test(org.junit.jupiter.api.Test)

Example 3 with RequestBindingException

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);
}
Also used : RequestBindingException(cn.taketoday.web.bind.RequestBindingException) MissingRequestPartException(cn.taketoday.web.bind.resolver.MissingRequestPartException) MissingPathVariableException(cn.taketoday.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpRequestMethodNotSupportedException(cn.taketoday.web.HttpRequestMethodNotSupportedException) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ConversionNotSupportedException(cn.taketoday.beans.ConversionNotSupportedException) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) BindException(cn.taketoday.validation.BindException) AsyncRequestTimeoutException(cn.taketoday.web.context.async.AsyncRequestTimeoutException) HttpMessageNotWritableException(cn.taketoday.http.converter.HttpMessageNotWritableException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 4 with RequestBindingException

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();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockServletConfig(cn.taketoday.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 5 with RequestBindingException

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;
    }
}
Also used : MatrixParam(cn.taketoday.web.annotation.MatrixParam) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Aggregations

RequestBindingException (cn.taketoday.web.bind.RequestBindingException)6 Test (org.junit.jupiter.api.Test)4 StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)3 MethodParameter (cn.taketoday.core.MethodParameter)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2 Nullable (cn.taketoday.lang.Nullable)2 MatrixParam (cn.taketoday.web.annotation.MatrixParam)2 ExceptionHandlerAnnotationExceptionHandler (cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler)2 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)2 ServletException (jakarta.servlet.ServletException)2 ConversionNotSupportedException (cn.taketoday.beans.ConversionNotSupportedException)1 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)1 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)1 HttpMessageNotWritableException (cn.taketoday.http.converter.HttpMessageNotWritableException)1 BindException (cn.taketoday.validation.BindException)1 HttpMediaTypeNotAcceptableException (cn.taketoday.web.HttpMediaTypeNotAcceptableException)1 HttpMediaTypeNotSupportedException (cn.taketoday.web.HttpMediaTypeNotSupportedException)1 HttpRequestMethodNotSupportedException (cn.taketoday.web.HttpRequestMethodNotSupportedException)1 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)1 MissingPathVariableException (cn.taketoday.web.bind.MissingPathVariableException)1