Skip to content

Commit

Permalink
fix: validation erorr message not shown in reponse, docs: CGD-391: sa…
Browse files Browse the repository at this point in the history
…mple repomse added in wallet APIs
  • Loading branch information
nitin-vavdiya committed Jul 19, 2023
1 parent 5ac9f2d commit 80ced85
Show file tree
Hide file tree
Showing 3 changed files with 515 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,36 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.smartsensesolutions.java.commons.specification.SpecificationUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;

/**
* The type Application config.
*/
@Configuration
@Slf4j
@RequiredArgsConstructor
public class ApplicationConfig implements WebMvcConfigurer {

private final SwaggerUiConfigProperties properties;
private final String resourceBundlePath;

@Autowired
public ApplicationConfig(@Value("${resource.bundle.path:classpath:i18n/language}") String resourceBundlePath, SwaggerUiConfigProperties properties) {
this.resourceBundlePath = resourceBundlePath;
this.properties = properties;
}

/**
* Object mapper object mapper.
Expand Down Expand Up @@ -71,4 +83,19 @@ public void addViewControllers(ViewControllerRegistry registry) {
log.info("Set landing page to path {}", StringEscapeUtils.escapeJava(redirectUri));
registry.addRedirectViewController("/", redirectUri);
}

@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource bean = new ReloadableResourceBundleMessageSource();
bean.setBasename(resourceBundlePath);
bean.setDefaultEncoding(StandardCharsets.UTF_8.name());
return bean;
}

@Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean beanValidatorFactory = new LocalValidatorFactoryBean();
beanValidatorFactory.setValidationMessageSource(messageSource());
return beanValidatorFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,28 @@

package org.eclipse.tractusx.managedidentitywallets.config;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.managedidentitywallets.exception.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* The type Exception handling.
*/
@RestControllerAdvice
@Slf4j
public class ExceptionHandling extends ResponseEntityExceptionHandler {
public class ExceptionHandling {

/**
* The constant TIMESTAMP.
Expand Down Expand Up @@ -98,6 +105,37 @@ ProblemDetail handleBadDataException(BadDataException e) {
return problemDetail;
}


/**
* Handle validation problem detail.
*
* @param e the e
* @return the problem detail
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
ProblemDetail handleValidation(MethodArgumentNotValidException e) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage());
problemDetail.setTitle("Invalid data provided");
problemDetail.setProperty(TIMESTAMP, System.currentTimeMillis());
problemDetail.setProperty("errors", handleValidationError(e.getFieldErrors()));
return problemDetail;
}

/**
* Handle validation problem detail.
*
* @param exception the exception
* @return the problem detail
*/
@ExceptionHandler(ConstraintViolationException.class)
ProblemDetail handleValidation(ConstraintViolationException exception) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, exception.getMessage());
problemDetail.setTitle("Invalid data provided");
problemDetail.setProperty(TIMESTAMP, System.currentTimeMillis());
problemDetail.setProperty("errors", exception.getConstraintViolations().stream().map(ConstraintViolation::getMessage).toList());
return problemDetail;
}

/**
* Handle duplicate credential problem problem detail.
*
Expand All @@ -112,6 +150,12 @@ ProblemDetail handleDuplicateCredentialProblem(RuntimeException e) {
return problemDetail;
}

/**
* Handle not found credential problem detail.
*
* @param e the e
* @return the problem detail
*/
@ExceptionHandler(CredentialNotFoundProblem.class)
ProblemDetail handleNotFoundCredentialProblem(CredentialNotFoundProblem e) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage());
Expand All @@ -120,6 +164,12 @@ ProblemDetail handleNotFoundCredentialProblem(CredentialNotFoundProblem e) {
return problemDetail;
}

/**
* Handle exception problem detail.
*
* @param e the e
* @return the problem detail
*/
@ExceptionHandler(Exception.class)
ProblemDetail handleException(Exception e) {
log.error("Error ", e);
Expand All @@ -128,4 +178,15 @@ ProblemDetail handleException(Exception e) {
problemDetail.setProperty(TIMESTAMP, System.currentTimeMillis());
return problemDetail;
}

/**
* @param fieldErrors errors
* @return ResponseEntity with error details
*/
private Map<String, String> handleValidationError(List<FieldError> fieldErrors) {

Map<String, String> messages = new HashMap<>();
fieldErrors.forEach(fieldError -> messages.put(fieldError.getField(), fieldError.getDefaultMessage()));
return messages;
}
}
Loading

0 comments on commit 80ced85

Please sign in to comment.