j00r6 / SPYAIR-Fansite-Project

1 stars 1 forks source link

[BE] Feat : Global #01 GlobalExceptionHandler #34

Closed j00r6 closed 6 months ago

j00r6 commented 8 months ago

구현내용

4df5af2d8e7f0d5b730312a1dbba2ae711778608

  1. BusinessLogicException.class > GlobalExceptionHandler.class 로 변경
  2. ExceptionCode 삭제 > Exception 별로 개별 Class 구현

적용

Exception 호출

MemberService.class

if (checkLoginIdDuplicate(request.getEmail())) throw new LoginIdDuplicateException("이미 존재하는 이메일입니다.");

LoginIdDuplicateException 구현

exception 패키지 내부에서 LoginIdDuplicateException.class 를 구현

public class LoginIdDuplicateException extends RuntimeException {
    public LoginIdDuplicateException(String message) {
        super(message);
    }
}

GlobalExceptionHandler 내부에 로직 구현

@ExceptionHandler(LoginIdDuplicateException.class)
public ResponseEntity<Message> handle(LoginIdDuplicateException e) {
    Message message = new Message(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    return new ResponseEntity<>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}

POSTMAN

요청

{
    "email" : "11",
    "password" : "11",
    "nickName" : "11",
    "phone" : "11",
    "name" : "11"
}

응답

{
    "message": "이미 존재하는 이메일입니다.",
    "status": "INTERNAL_SERVER_ERROR"
}
j00r6 commented 8 months ago

차이점 분석

기존

MemberController.class

// 이메일 중복 체크
if (service.checkLoginIdDuplicate(request.getEmail())) {
        return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body("이메일 중복!");
}

// 닉네임 중복 체크
if (service.checkNickNameDuplicate(request.getNickName())) {
        return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body("닉네임 중복!");
}

장점

단점


현재

코드는 상단 참조

장점

단점


Ref.

참조 1 - GlobalExceptionHandler 구현 (블로그) 참조 2 - ExceptionCode Custom 심화과정 (블로그) 참조 3 - GlobalExceptionHandler (블로그) 참조 4 - ResponseEntity 에 대해 (블로그)