Skip to content

Commit

Permalink
feat: Error 관련 코드, 전역 예외 처리 핸들러 추가 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 1, 2023
1 parent c39b500 commit 6204b38
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/project/mapdagu/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.*;


@Getter
@RequiredArgsConstructor
public enum ErrorCode {

TOKEN_NOT_EXIST(-1000, "토큰이 존재하지 않습니다.");
TOKEN_NOT_EXIST(NOT_FOUND, "토큰이 존재하지 않습니다.");

private final int code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.project.mapdagu.error.dto;

public record ErrorResponse(int code, String message) {
public static ErrorResponse of(int code, String message){
return new ErrorResponse(code, message);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.project.mapdagu.error.exception;

import com.project.mapdagu.error.dto.ErrorResponse;
import com.project.mapdagu.error.exception.custom.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler
public ResponseEntity<ErrorResponse> handle(final BusinessException e) {
log.info("businessException: {}", e);
return ResponseEntity.badRequest().body(ErrorResponse.of(e.getCode(), e.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.project.mapdagu.error.exception.custom;

import com.project.mapdagu.error.ErrorCode;
import lombok.Getter;

@Getter
public class BusinessException extends RuntimeException{

private final int code;

public BusinessException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
}
}

0 comments on commit 6204b38

Please sign in to comment.