modify exception handling
This commit is contained in:
parent
aad8586745
commit
9979b5294e
|
@ -31,7 +31,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
@ -67,11 +66,11 @@ public class RegistrationController {
|
||||||
|
|
||||||
@RequestMapping(value = "/user/registration", method = RequestMethod.POST)
|
@RequestMapping(value = "/user/registration", method = RequestMethod.POST)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request) {
|
public GenericResponse registerUserAccount(@Valid final UserDto accountDto, final HttpServletRequest request) {
|
||||||
LOGGER.debug("Registering user account with information: {}", accountDto);
|
LOGGER.debug("Registering user account with information: {}", accountDto);
|
||||||
if (result.hasErrors()) {
|
// if (result.hasErrors()) {
|
||||||
return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
|
// return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
|
||||||
}
|
// }
|
||||||
final User registered = createUserAccount(accountDto);
|
final User registered = createUserAccount(accountDto);
|
||||||
if (registered == null) {
|
if (registered == null) {
|
||||||
return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
|
return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
|
||||||
|
|
|
@ -7,6 +7,8 @@ import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.mail.MailAuthenticationException;
|
import org.springframework.mail.MailAuthenticationException;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.context.request.WebRequest;
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
@ -24,6 +26,15 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
|
// 400
|
||||||
|
@Override
|
||||||
|
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
||||||
|
logger.error("400 Status Code", ex);
|
||||||
|
BindingResult result = ex.getBindingResult();
|
||||||
|
final GenericResponse bodyOfResponse = new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
|
||||||
|
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||||
|
}
|
||||||
|
|
||||||
// 404
|
// 404
|
||||||
@ExceptionHandler({ UserNotFoundException.class })
|
@ExceptionHandler({ UserNotFoundException.class })
|
||||||
public ResponseEntity<Object> handleUserNotFound(final RuntimeException ex, final WebRequest request) {
|
public ResponseEntity<Object> handleUserNotFound(final RuntimeException ex, final WebRequest request) {
|
||||||
|
|
Loading…
Reference in New Issue