-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 행사 커스텀 예외 처리 * feat: 커스텀 예외 적용
- Loading branch information
Showing
10 changed files
with
131 additions
and
14 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
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
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
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
10 changes: 10 additions & 0 deletions
10
server/src/main/java/server/haengdong/exception/ErrorResponse.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,10 @@ | ||
package server.haengdong.exception; | ||
|
||
public record ErrorResponse( | ||
String message | ||
) { | ||
|
||
public static ErrorResponse of(HaengdongErrorCode errorCode) { | ||
return new ErrorResponse(errorCode.getMessage()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
server/src/main/java/server/haengdong/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,43 @@ | ||
package server.haengdong.exception; | ||
|
||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
public ResponseEntity<ErrorResponse> haengdongException() { | ||
return ResponseEntity.badRequest() | ||
.body(ErrorResponse.of(HaengdongErrorCode.BAD_REQUEST)); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
String errorMessage = e.getFieldErrors().stream() | ||
.map(error -> error.getField() + " " + error.getDefaultMessage()) | ||
.collect(Collectors.joining(", ")); | ||
|
||
return ResponseEntity.badRequest() | ||
.body(new ErrorResponse(errorMessage)); | ||
} | ||
|
||
@ExceptionHandler(HaengdongException.class) | ||
public ResponseEntity<ErrorResponse> haengdongException(HaengdongException e) { | ||
return ResponseEntity.status(e.getStatusCode()) | ||
.body(ErrorResponse.of(e.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(Exception e) { | ||
log.error(e.getMessage(), e); | ||
return ResponseEntity.internalServerError() | ||
.body(ErrorResponse.of(HaengdongErrorCode.INTERNAL_SERVER_ERROR)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
server/src/main/java/server/haengdong/exception/HaengdongErrorCode.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,24 @@ | ||
package server.haengdong.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum HaengdongErrorCode { | ||
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
DUPLICATED_MEMBER_ACTION(HttpStatus.BAD_REQUEST, "올바르지 않은 인원 요청입니다."), | ||
INVALID_MEMBER_ACTION(HttpStatus.BAD_REQUEST, "잘못된 맴버 액션입니다."), | ||
|
||
NOT_FOUND_EVENT(HttpStatus.NOT_FOUND, "존재하지 않는 행사입니다."), | ||
|
||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부에서 에러가 발생했습니다."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
|
||
HaengdongErrorCode(HttpStatus httpStatus, String message) { | ||
this.httpStatus = httpStatus; | ||
this.message = message; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
server/src/main/java/server/haengdong/exception/HaengdongException.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,32 @@ | ||
package server.haengdong.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatusCode; | ||
|
||
@Getter | ||
public class HaengdongException extends RuntimeException { | ||
|
||
private final HaengdongErrorCode errorCode; | ||
private final String message; | ||
|
||
public HaengdongException(HaengdongErrorCode errorCode) { | ||
this(errorCode, null); | ||
} | ||
|
||
public HaengdongException(HaengdongErrorCode errorCode, String message) { | ||
this.errorCode = errorCode; | ||
this.message = message; | ||
} | ||
|
||
public HttpStatusCode getStatusCode() { | ||
return errorCode.getHttpStatus(); | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
if (message == null) { | ||
return errorCode.getMessage(); | ||
} | ||
return message; | ||
} | ||
} |
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
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