-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
33 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 30 additions & 2 deletions
32
photo-service/src/main/java/kr/mafoo/photo/config/WebExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
package kr.mafoo.photo.config; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import kr.mafoo.photo.controller.dto.response.ErrorResponse; | ||
import kr.mafoo.photo.exception.DomainException; | ||
import kr.mafoo.photo.exception.ErrorCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.bind.support.WebExchangeBindException; | ||
|
||
@ControllerAdvice | ||
@RestControllerAdvice | ||
public class WebExceptionHandler { | ||
@ExceptionHandler(DomainException.class) | ||
public ResponseEntity<ErrorResponse> handleDomainException(DomainException exception) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(ErrorResponse.fromErrorCode(exception.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler({MethodArgumentNotValidException.class, | ||
ConstraintViolationException.class, | ||
WebExchangeBindException.class}) | ||
public ResponseEntity<ErrorResponse> validException(Exception ex) { | ||
String errorMessage = "์ ๋ ฅ๊ฐ ๊ฒ์ฆ ์ค๋ฅ: "; | ||
if (ex instanceof MethodArgumentNotValidException mex) { | ||
errorMessage += mex.getBindingResult().getAllErrors().get(0).getDefaultMessage(); | ||
} else if (ex instanceof ConstraintViolationException cvex) { | ||
errorMessage += cvex.getConstraintViolations().iterator().next().getMessage(); | ||
} else if (ex instanceof WebExchangeBindException wex) { | ||
errorMessage += wex.getAllErrors().get(0).getDefaultMessage(); | ||
} else { | ||
errorMessage += "์ ์ ์๋ ์ค๋ฅ"; | ||
} | ||
ErrorResponse response = new ErrorResponse( | ||
ErrorCode.REQUEST_INPUT_NOT_VALID.getCode(), | ||
errorMessage | ||
); | ||
|
||
return ResponseEntity | ||
.badRequest() | ||
.body(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters