Created
December 17, 2017 18:10
-
-
Save diegosilva13/b403c12ec3e5642911f0b03ade7ec58b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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