Skip to content

Commit

Permalink
#33 [Add] 예외처리 틀
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 15, 2022
1 parent 69b0787 commit e83f41b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/mpnp/baechelin/exception/CustomException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mpnp.baechelin.exception;

import lombok.Getter;

@Getter
public class CustomException extends RuntimeException{
private ErrorCode errorCode;

public CustomException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/mpnp/baechelin/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mpnp.baechelin.exception;

import lombok.Builder;
import lombok.Getter;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;

@Getter
@Builder
public class ErrorResponse {
private final LocalDateTime timestamp = LocalDateTime.now();
private final int status;
private final String error;
private final String code;
private final String message;

public static ResponseEntity<ErrorResponse> toResponseEntity(ErrorCode errorCode) {
return ResponseEntity
.status(errorCode.getStatus())
.body(ErrorResponse.builder()
.status(errorCode.getStatus())
.error(errorCode.getCode())
.code(errorCode.name())
.message(errorCode.getMessage())
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mpnp.baechelin.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.mvc.method.annotation.ResponseEntityExceptionHandler;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = CustomException.class)
protected ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
log.error("handleCustomException throw CustomException : {}", e.getErrorCode());
return ErrorResponse.toResponseEntity(e.getErrorCode());
}
}

0 comments on commit e83f41b

Please sign in to comment.