Search in sources :

Example 1 with ConversionNotSupportedException

use of org.springframework.beans.ConversionNotSupportedException in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleConversionNotSupportedException.

@Test
public void handleConversionNotSupportedException() throws Exception {
    ConversionNotSupportedException ex = new ConversionNotSupportedException(new Object(), String.class, new Exception());
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertThat(mav).as("No ModelAndView returned").isNotNull();
    assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
    assertThat(response.getStatus()).as("Invalid status code").isEqualTo(500);
    // SPR-9653
    assertThat(request.getAttribute("jakarta.servlet.error.exception")).isSameAs(ex);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Test(org.junit.jupiter.api.Test)

Example 2 with ConversionNotSupportedException

use of org.springframework.beans.ConversionNotSupportedException in project alien4cloud by alien4cloud.

the class AbstractTypeNodeParser method parseAndSetValue.

protected void parseAndSetValue(BeanWrapper target, String key, Node valueNode, ParsingContextExecution context, MappingTarget mappingTarget) {
    // let's store the parent in the context for future use
    context.setParent(target.getWrappedInstance());
    if (mappingTarget.getPath().equals("null")) {
        // if the path is null, we just to do nothing with the stuff
        return;
    }
    Entry<BeanWrapper, String> entry = findWrapperPropertyByPath(context.getRoot(), target, mappingTarget.getPath());
    BeanWrapper realTarget = entry.getKey();
    String propertyName = entry.getValue();
    Object value = ((INodeParser<?>) mappingTarget.getParser()).parse(valueNode, context);
    ParsingContextExecution.setParent(target, value);
    if (!propertyName.equals("void")) {
        // property named 'void' means : process the parsing but do not set anything
        try {
            realTarget.setPropertyValue(propertyName, value);
        } catch (ConversionNotSupportedException e) {
            context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.SYNTAX_ERROR, "Invalid yaml type for property", valueNode.getStartMark(), "", valueNode.getEndMark(), toscaType));
        } catch (NotWritablePropertyException e) {
            log.warn("Error while setting property for yaml parsing.", e);
            context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.ALIEN_MAPPING_ERROR, "Invalid definition for type", valueNode.getStartMark(), e.getPropertyName(), valueNode.getEndMark(), toscaType));
        }
    }
    if (mappingTarget instanceof KeyValueMappingTarget) {
        KeyValueMappingTarget kvmt = (KeyValueMappingTarget) mappingTarget;
        BeanWrapper keyBeanWrapper = realTarget;
        try {
            if (!(keyBeanWrapper.getPropertyValue(kvmt.getKeyPath()) != null && propertyName.equals(key))) {
                keyBeanWrapper.setPropertyValue(kvmt.getKeyPath(), key);
            }
        } catch (ConversionNotSupportedException e) {
            context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.SYNTAX_ERROR, "Invalid yaml type for property", valueNode.getStartMark(), "", valueNode.getEndMark(), toscaType));
        } catch (NotWritablePropertyException e) {
            log.warn("Error while setting key to property for yaml parsing.", e);
            context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.ALIEN_MAPPING_ERROR, "Invalid definition for type", valueNode.getStartMark(), e.getPropertyName(), valueNode.getEndMark(), toscaType));
        }
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException)

Example 3 with ConversionNotSupportedException

use of org.springframework.beans.ConversionNotSupportedException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method conversionNotSupported.

@Test
public void conversionNotSupported() {
    Exception ex = new ConversionNotSupportedException(new Object(), Object.class, null);
    testException(ex);
}
Also used : MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Test(org.junit.jupiter.api.Test)

Example 4 with ConversionNotSupportedException

use of org.springframework.beans.ConversionNotSupportedException in project spring-framework by spring-projects.

the class AbstractNamedValueMethodArgumentResolver method resolveArgument.

@Override
@Nullable
public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
    NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
    MethodParameter nestedParameter = parameter.nestedIfOptional();
    Object resolvedName = resolveEmbeddedValuesAndExpressions(namedValueInfo.name);
    if (resolvedName == null) {
        throw new IllegalArgumentException("Specified name must not resolve to null: [" + namedValueInfo.name + "]");
    }
    Object arg = resolveName(resolvedName.toString(), nestedParameter, webRequest);
    if (arg == null) {
        if (namedValueInfo.defaultValue != null) {
            arg = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
        } else if (namedValueInfo.required && !nestedParameter.isOptional()) {
            handleMissingValue(namedValueInfo.name, nestedParameter, webRequest);
        }
        arg = handleNullValue(namedValueInfo.name, arg, nestedParameter.getNestedParameterType());
    } else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
        arg = resolveEmbeddedValuesAndExpressions(namedValueInfo.defaultValue);
    }
    if (binderFactory != null) {
        WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
        try {
            arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
        } catch (ConversionNotSupportedException ex) {
            throw new MethodArgumentConversionNotSupportedException(arg, ex.getRequiredType(), namedValueInfo.name, parameter, ex.getCause());
        } catch (TypeMismatchException ex) {
            throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(), namedValueInfo.name, parameter, ex.getCause());
        }
        // Check for null value after conversion of incoming argument value
        if (arg == null && namedValueInfo.defaultValue == null && namedValueInfo.required && !nestedParameter.isOptional()) {
            handleMissingValueAfterConversion(namedValueInfo.name, nestedParameter, webRequest);
        }
    }
    handleResolvedValue(arg, namedValueInfo.name, parameter, mavContainer, webRequest);
    return arg;
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) TypeMismatchException(org.springframework.beans.TypeMismatchException) MethodParameter(org.springframework.core.MethodParameter) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Nullable(org.springframework.lang.Nullable)

Aggregations

ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)4 TypeMismatchException (org.springframework.beans.TypeMismatchException)3 Test (org.junit.jupiter.api.Test)2 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)2 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)2 BindException (org.springframework.validation.BindException)2 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)2 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)2 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)2 MissingPathVariableException (org.springframework.web.bind.MissingPathVariableException)2 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)2 ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)2 AsyncRequestTimeoutException (org.springframework.web.context.request.async.AsyncRequestTimeoutException)2 MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)2 NoHandlerFoundException (org.springframework.web.servlet.NoHandlerFoundException)2 ServletException (jakarta.servlet.ServletException)1 BeanWrapper (org.springframework.beans.BeanWrapper)1 NotWritablePropertyException (org.springframework.beans.NotWritablePropertyException)1 MethodParameter (org.springframework.core.MethodParameter)1 Nullable (org.springframework.lang.Nullable)1