protected Response handleValidationException ( Throwable exception , Throwable catched ) { Throwable t = catched ; Map < String , List < String > > errorsMap = null ; if ( exception instanceof ConstraintViolationException ) { ConstraintViolationException constraintViolationException = ( ConstraintViolationException ) exception ; Set < ConstraintViolation < ? > > violations = constraintViolationException . getConstraintViolations ( ) ; errorsMap = new HashMap <> ( ) ; for ( ConstraintViolation < ? > violation : violations ) { Iterator < Node > it = violation . getPropertyPath ( ) . iterator ( ) ; String fieldName = null ; // Getting fieldname from the exception while ( it . hasNext ( ) ) { fieldName = it . next ( ) . toString ( ) ; } List < String > errorsList = errorsMap . get ( fieldName ) ; if ( errorsList == null ) { errorsList = new ArrayList <> ( ) ; errorsMap . put ( fieldName , errorsList ) ; } errorsList . add ( violation . getMessage ( ) ) ; } t = new ValidationException ( errorsMap . toString ( ) , catched ) ; } ValidationErrorUserException error = new ValidationErrorUserException ( t ) ; return createResponse ( t , error , errorsMap ) ; }
Exception handling for validation exception .