-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/com/cotato/team2/team2/exception/CustomErrorResponse.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.cotato.team2.team2.exception; | ||
|
||
public record CustomErrorResponse( | ||
int code, | ||
String message, | ||
String method, | ||
String requestURI | ||
) { | ||
public static CustomErrorResponse of(int code, String message, String method, String requestURI) { | ||
return new CustomErrorResponse(code, message, method, requestURI); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/cotato/team2/team2/exception/GlobalExceptionHandler.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.cotato.team2.team2.exception; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(BusinessException.class) | ||
public ResponseEntity<CustomErrorResponse> handleBusinessException(HttpServletRequest request, BusinessException e) { | ||
log.error("BusinessException 발생: {} <{}>", e.getCode(), e.getMessage()); | ||
CustomErrorResponse errorResponse = CustomErrorResponse.of(e.getCode(), e.getMessage(), request.getMethod(), request.getRequestURI()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); | ||
} | ||
} |