Skip to content

Instantly share code, notes, and snippets.

@diegosilva13
Created December 17, 2017 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegosilva13/b403c12ec3e5642911f0b03ade7ec58b to your computer and use it in GitHub Desktop.
Save diegosilva13/b403c12ec3e5642911f0b03ade7ec58b to your computer and use it in GitHub Desktop.
package com.coderef.delivery.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;
@ControllerAdvice
public class ExceptionHandlerController {
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<?> validateError(ConstraintViolationException ex){
return ResponseEntity.badRequest().body(ex.getConstraintViolations().stream().map(cv -> cv.getMessage()).collect(Collectors.toList()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<?> otherErrors(Exception ex){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment