Skip to content

Commit

Permalink
Merge pull request eugenp#170 from Doha2012/master
Browse files Browse the repository at this point in the history
modify exception handling
  • Loading branch information
Eugen committed Mar 23, 2015
2 parents 8abb2ee + 9979b52 commit 6260f49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -67,11 +66,11 @@ public RegistrationController() {

@RequestMapping(value = "/user/registration", method = RequestMethod.POST)
@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);
if (result.hasErrors()) {
return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
}
// if (result.hasErrors()) {
// return new GenericResponse(result.getFieldErrors(), result.getGlobalErrors());
// }
final User registered = createUserAccount(accountDto);
if (registered == null) {
return new GenericResponse("email", messages.getMessage("message.regError", null, request.getLocale()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
Expand All @@ -24,6 +26,15 @@ public RestResponseEntityExceptionHandler() {

// 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
@ExceptionHandler({ UserNotFoundException.class })
public ResponseEntity<Object> handleUserNotFound(final RuntimeException ex, final WebRequest request) {
Expand Down

0 comments on commit 6260f49

Please sign in to comment.