Skip to content

Commit

Permalink
Merge pull request #10 from PawWithU/feat/8-GlobalException
Browse files Browse the repository at this point in the history
[Feature] 전역적 예외 처리 설정
  • Loading branch information
hojeong2747 authored Oct 22, 2023
2 parents 81e4c6b + 1e8555c commit 58ac813
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/pawwithu/connectdog/error/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.pawwithu.connectdog.error;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum ErrorCode {

TOKEN_NOT_EXIST("T1", "토큰이 존재하지 않습니다."),
TOKEN_IS_EXPIRED("T2", "만료된 토큰입니다."),
INVALID_TOKEN("T3", "잘못된 토큰입니다.");

private final String code;
private final String message;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pawwithu.connectdog.error.dto;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.pawwithu.connectdog.error.exception;

import com.pawwithu.connectdog.error.dto.ErrorResponse;
import com.pawwithu.connectdog.error.exception.custom.BadRequestException;
import com.pawwithu.connectdog.error.exception.custom.TokenException;
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;

@Slf4j
@RestControllerAdvice
public class GlobalControllerAdvice {

@ExceptionHandler
public ResponseEntity<String> handle(final Exception e) {
log.error("Internal Error occurred", e);
return ResponseEntity.internalServerError().body(e.getMessage());
}

@ExceptionHandler
public ResponseEntity<ErrorResponse> handle(final BadRequestException e) {
log.info("Bad Request: {}", e);
return ResponseEntity.badRequest().body(ErrorResponse.of(e.getCode(), e.getMessage()));
}

@ExceptionHandler
public ResponseEntity<ErrorResponse> handle(final TokenException e) {
log.info("Invalid Token: {}", e);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ErrorResponse.of(e.getCode(), e.getMessage()));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pawwithu.connectdog.error.exception.custom;

import com.pawwithu.connectdog.error.ErrorCode;
import lombok.Getter;

@Getter
public class BadRequestException extends RuntimeException {

private final String code;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.pawwithu.connectdog.error.exception.custom;

import com.pawwithu.connectdog.error.ErrorCode;
import lombok.Getter;

@Getter
public class TokenException extends RuntimeException {

private final String code;

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


}

0 comments on commit 58ac813

Please sign in to comment.