Skip to content

Commit

Permalink
feat(exception): validation exception 처리 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhboy committed Apr 22, 2024
1 parent 6a38f2d commit 0be2e54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sickgyun.server.common.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand All @@ -17,6 +18,22 @@ public ResponseEntity<ErrorResponse> handleDefineException(SickgyunException exc
.body(new ErrorResponse(exception.getStatus().value(), exception.getMessage()));
}

@ExceptionHandler({MethodArgumentNotValidException.class})
public ResponseEntity<ErrorResponse> handleDefineException(MethodArgumentNotValidException exception) {
LoggingUtils.warn(exception);

String message;

if (exception.getFieldError() == null) {
message = "";
} else {
message = exception.getFieldError().getDefaultMessage();
}

return ResponseEntity.status(400)
.body(new ErrorResponse(400, message));
}

@ExceptionHandler({RuntimeException.class})
public ResponseEntity<ErrorResponse> handleDefineException(RuntimeException exception) {
LoggingUtils.error(exception);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sickgyun.server.common.logging;

import org.springframework.web.bind.MethodArgumentNotValidException;

import com.sickgyun.server.common.exception.SickgyunException;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,6 +13,11 @@ public static void warn(SickgyunException exception) {
log.warn(message + "\n \t {}", exception);
}

public static void warn(MethodArgumentNotValidException exception) {
String message = getExceptionMessage(exception.getMessage());
log.warn(message + "\n \t {}", exception);
}

private static String getExceptionMessage(String message) {
if (message == null || message.isBlank()) {
return "";
Expand Down

0 comments on commit 0be2e54

Please sign in to comment.