From ae5d1dde0918241fc6f7904e032c05d818f5f49b Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 25 Mar 2015 08:29:22 +0200 Subject: [PATCH] handle bindexception --- .../src/main/webapp/WEB-INF/view/registration.jsp | 12 +++++++++++- .../error/RestResponseEntityExceptionHandler.java | 13 +++++++++++-- .../src/main/webapp/WEB-INF/view/registration.jsp | 12 +++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp index 43e4e158c4..8b23b68dd7 100644 --- a/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-and-registration-frontend/src/main/webapp/WEB-INF/view/registration.jsp @@ -83,9 +83,19 @@ function register(){ { window.location.href = ""; } - else{ + else if(data.responseJSON.error.indexOf("InternalError") > -1){ window.location.href = "" + "?message=" + data.responseJSON.message; } + else{ + var errors = $.parseJSON(data.responseJSON.message); + $.each( errors, function( index,item ){ + $("#"+item.field+"Error").show().html(item.defaultMessage); + }); + errors = $.parseJSON(data.responseJSON.error); + $.each( errors, function( index,item ){ + $("#globalError").show().append(item.defaultMessage+"
"); + }); + } }); } diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java index 2c220f40e0..ee239fd598 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java @@ -7,6 +7,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.mail.MailAuthenticationException; +import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -27,6 +28,14 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH // API // 400 + @Override + protected ResponseEntity handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { + logger.error("400 Status Code", ex); + final BindingResult result = ex.getBindingResult(); + final GenericResponse bodyOfResponse = new GenericResponse(result.getFieldErrors(), result.getGlobalErrors()); + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + } + @Override protected ResponseEntity handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { logger.error("400 Status Code", ex); @@ -48,14 +57,14 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH public ResponseEntity handleMail(final RuntimeException ex, final WebRequest request) { logger.error("500 Status Code", ex); final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.email.config.error", null, request.getLocale()), "MailError"); - return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); + return new ResponseEntity(bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler({ Exception.class }) public ResponseEntity handleInternal(final RuntimeException ex, final WebRequest request) { logger.error("500 Status Code", ex); final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.error", null, request.getLocale()), "InternalError"); - return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); + return new ResponseEntity(bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp index a7f3821342..6660f9ac52 100644 --- a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/registration.jsp @@ -84,9 +84,19 @@ function register(){ { window.location.href = ""; } - else{ + else if(data.responseJSON.error.indexOf("InternalError") > -1){ window.location.href = "" + "?message=" + data.responseJSON.message; } + else{ + var errors = $.parseJSON(data.responseJSON.message); + $.each( errors, function( index,item ){ + $("#"+item.field+"Error").show().html(item.defaultMessage); + }); + errors = $.parseJSON(data.responseJSON.error); + $.each( errors, function( index,item ){ + $("#globalError").show().append(item.defaultMessage+"
"); + }); + } }); }