Skip to content

Commit

Permalink
refactor: Valid 에러 위치 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
SHEOMM committed Sep 7, 2024
1 parent fdec831 commit 3e9c6e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

import com.waruru.areyouhere.common.annotation.SlackNotification;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
Expand All @@ -23,6 +28,8 @@ public ResponseEntity<HttpStatus> handle404Exception(HttpServletRequest request,
return RESPONSE_NOT_FOUND;
}



@SlackNotification
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
Expand All @@ -39,5 +46,18 @@ public ErrorResponse handleException(HttpServletRequest request, Exception e) {
}


@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({HandlerMethodValidationException.class, MethodArgumentNotValidException.class})
public Map<String, String> handleValidationExceptions(
MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public ResponseEntity<HttpStatus> courseNotFoundFoundHandler() {
return RESPONSE_NOT_FOUND;
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<HttpStatus> BadRequestHandler(){
return RESPONSE_BAD_REQUEST;
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<HttpStatus> methodArgumentNotValidHandler(){
return RESPONSE_BAD_REQUEST;
}

@ExceptionHandler(AttendeeNotFoundException.class)
public ResponseEntity<HttpStatus> attendeeNotFoundHandler(){
return RESPONSE_BAD_REQUEST;
Expand Down

0 comments on commit 3e9c6e6

Please sign in to comment.