Skip to content

Commit

Permalink
#3 feat: Create GlobalException init settings
Browse files Browse the repository at this point in the history
  • Loading branch information
thals0 committed Jul 16, 2023
1 parent f76bf29 commit 386907b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.knockknock.global.exception;


import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

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

@Getter
@AllArgsConstructor
public enum GlobalErrorCode {
// Member
// 400 BAD_REQUEST - 잘못된 요청
NOT_VALID_EMAIL(BAD_REQUEST, "유효하지 않은 이메일 입니다."),
PASSWORD_MISMATCH(BAD_REQUEST, "비밀번호가 일치하지 않습니다."),
DUPLICATE_PASSWORD(BAD_REQUEST, "비밀번호가 동일합니다."),
DUPLICATE_NICK_NAME(BAD_REQUEST, "닉네임이 동일합니다."),
NOT_VALID_NICKNAME(BAD_REQUEST, "영문, 한글, 숫자를 포함한 2~ 16글자를 입력해 주세요."),
NOT_VALID_PASSWORD(BAD_REQUEST, "영문, 숫자, 특수문자를 포함한 8~20 글자를 입력해 주세요."),
NOT_VALID_PHONENUMBER(BAD_REQUEST, "11자리 이내의 번호를 '-'를 제외한 숫자만 입력해 주세요."),
// 401 Unauthorized - 권한 없음
INVALID_TOKEN(UNAUTHORIZED, "토큰이 유효하지 않습니다"),
// 404 Not Found - 찾을 수 없음
EMAIL_NOT_FOUND(NOT_FOUND, "존재하지 않는 이메일 입니다."),
NEED_AGREE_REQUIRE_TERMS(NOT_FOUND, "필수 약관에 동의해 주세요"),
USER_NOT_FOUND(NOT_FOUND, "등록된 사용자가 없습니다"),
USERINFO_NOT_FOUND(NOT_FOUND, "등록된 사용자 정보가 없습니다"),
// 409 CONFLICT : Resource 를 찾을 수 없음
DUPLICATE_EMAIL(CONFLICT, "중복된 이메일이 존재합니다"),
EXPIRED_REFRESH_TOKEN(BAD_REQUEST, "쿠키에 토큰이 없습니다."),

// Friend
FRIEND_NOT_FOUND(NOT_FOUND, "등록된 친구가 없습니다"),

// Notification
NOTIFICATION_NOT_FOUND(NOT_FOUND, "등록된 연락 일정이 없습니다"),
;



private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.knockknock.global.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class GlobalException extends RuntimeException {
private final GlobalErrorCode errorCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.knockknock.global.exception;

import com.example.lablink.global.message.ResponseMessage;
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(value = { GlobalException.class })
protected ResponseEntity<ResponseMessage> handleCustomException(GlobalException e) {
log.error("handleCustomException throw CustomException : {}", e.getErrorCode());
return ResponseMessage.ErrorResponse(e.getErrorCode());
}

}

0 comments on commit 386907b

Please sign in to comment.