Skip to content

Commit 2abdf14

Browse files
author
Eugen
committed
Merge pull request eugenp#173 from Doha2012/master
handle bindexception
2 parents 52ab949 + ae5d1dd commit 2abdf14

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,19 @@ function register(){
8383
{
8484
window.location.href = "<c:url value="/emailError.html"></c:url>";
8585
}
86-
else{
86+
else if(data.responseJSON.error.indexOf("InternalError") > -1){
8787
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data.responseJSON.message;
8888
}
89+
else{
90+
var errors = $.parseJSON(data.responseJSON.message);
91+
$.each( errors, function( index,item ){
92+
$("#"+item.field+"Error").show().html(item.defaultMessage);
93+
});
94+
errors = $.parseJSON(data.responseJSON.error);
95+
$.each( errors, function( index,item ){
96+
$("#globalError").show().append(item.defaultMessage+"<br>");
97+
});
98+
}
8999
});
90100
}
91101
</script>

‎spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.http.HttpStatus;
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.mail.MailAuthenticationException;
10+
import org.springframework.validation.BindException;
1011
import org.springframework.validation.BindingResult;
1112
import org.springframework.web.bind.MethodArgumentNotValidException;
1213
import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -27,6 +28,14 @@ public RestResponseEntityExceptionHandler() {
2728
// API
2829

2930
// 400
31+
@Override
32+
protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
33+
logger.error("400 Status Code", ex);
34+
final BindingResult result = ex.getBindingResult();
35+
final GenericResponse bodyOfResponse = new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
36+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
37+
}
38+
3039
@Override
3140
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
3241
logger.error("400 Status Code", ex);
@@ -48,14 +57,14 @@ public ResponseEntity<Object> handleUserNotFound(final RuntimeException ex, fina
4857
public ResponseEntity<Object> handleMail(final RuntimeException ex, final WebRequest request) {
4958
logger.error("500 Status Code", ex);
5059
final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.email.config.error", null, request.getLocale()), "MailError");
51-
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
60+
return new ResponseEntity<Object>(bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
5261
}
5362

5463
@ExceptionHandler({ Exception.class })
5564
public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
5665
logger.error("500 Status Code", ex);
5766
final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.error", null, request.getLocale()), "InternalError");
58-
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
67+
return new ResponseEntity<Object>(bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
5968
}
6069

6170
}

‎spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,19 @@ function register(){
8484
{
8585
window.location.href = "<c:url value="/emailError.html"></c:url>";
8686
}
87-
else{
87+
else if(data.responseJSON.error.indexOf("InternalError") > -1){
8888
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data.responseJSON.message;
8989
}
90+
else{
91+
var errors = $.parseJSON(data.responseJSON.message);
92+
$.each( errors, function( index,item ){
93+
$("#"+item.field+"Error").show().html(item.defaultMessage);
94+
});
95+
errors = $.parseJSON(data.responseJSON.error);
96+
$.each( errors, function( index,item ){
97+
$("#globalError").show().append(item.defaultMessage+"<br>");
98+
});
99+
}
90100
});
91101
}
92102
</script>

0 commit comments

Comments
 (0)