Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/example/week2/builder/App1.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.example.week2.builder;

import com.example.week2.exception.CustomException;
import com.example.week2.exception.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App1 {

private static final Logger log = LoggerFactory.getLogger(App1.class); //@Slf4j 입력 시 자동 생성

public static void main(String[] args) {
log.error("에러!");

throw new CustomException(ErrorCode.INVALID_REQUEST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CustomException extends RuntimeException{
private final ErrorCode errorCode;

public CustomException(ErrorCode errorCode) {
super(errorCode.getMessage());
super(errorCode.getMessage()); //에러 메세지 출력
this.errorCode = errorCode;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/week2/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
@AllArgsConstructor
public enum ErrorCode {

INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청이 들어왔습니다"),;

INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청이 들어왔습니다"),
SEJONG_UNIV(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "가나다라마바사"); //에러 타입과 에러메세지 형식

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@Builder
@AllArgsConstructor
public class ErrorResponse {

private ErrorCode errorCode;
private String errorMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public void throwIllegalArgumentException() {
throw new IllegalArgumentException();
}

@GetMapping("/custom")
public void throwCustomException() {throw new CustomException(ErrorCode.SEJONG_UNIV);}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.example.week2.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.View;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

private final View error;

public GlobalExceptionHandler(View error) {
this.error = error;
}

@ExceptionHandler(NullPointerException.class)
public String handleNullPointerException() {
log.error("NullPointer Exception 처리 시작");
Expand All @@ -19,4 +27,20 @@ public String handleInternalError() {
log.error("InternalError 처리 시작");
return "InternalError 핸들링";
}

@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e){
log.error("CustomException 발생: {}", e.getMessage(), e);

ErrorCode errorCode = e.getErrorCode();

ErrorResponse response = ErrorResponse.builder()
.errorCode(errorCode)
.errorMessage(errorCode.getMessage())
.build();

return ResponseEntity.status(errorCode.getStatus()).body(response);
}


}