[Spring] Custome Exception ๋งŒ๋“ค๊ธฐ
Back-end/Spring

[Spring] Custome Exception ๋งŒ๋“ค๊ธฐ

728x90

๐Ÿ“‘ ๊ฐœ์š”

์ด์ „ ํ”„๋กœ์ ํŠธ์—์„  ๋ฐฑ์—”๋“œ ๋„๋ฉ”์ธ ๋ณ„๋กœ ๊ฐœ๋ฐœ ํŒŒํŠธ๋ฅผ ๋‚˜๋ˆ„๊ณ  ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ์ž ์•Œ์•„์„œ ํ–ˆ๋‹ค.

 

์‹ฌ์ง€์–ด ๋ฌธ์„œํ™”๋„ ์ œ๋Œ€๋กœ ํ•ด๋†“์ง€ ์•Š์•„ ํ•ด๋‹น ์˜ˆ์™ธ๊ฐ€ ์™œ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์ž˜ ์ „๋‹ฌ์ด ๋˜์ง€ ์•Š์•˜๊ณ 

์™œ, ์–ด๋””์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ์ธ์ง€ ์ฐพ๋Š” ๊ฒƒ์ด ๊ต‰์žฅํžˆ ํž˜๋“ค์—ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์—์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ๋‚ด๊ฐ€ ๊ฐœ๋ฐœํ•œ ํŒŒํŠธ์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๊ฐ€ ์•„๋‹˜์—๋„

์—‰๋šฑํ•œ ๊ณณ์—์„œ ๋ฌธ์ œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์‹œ๊ฐ„์„ ํ—ˆ๋น„ํ•œ ๊ฒฝํ—˜์ด ์žˆ์—ˆ๋‹ค.

 

์ด๋Ÿฐ ๊ฒฝํ—˜์„ ํ†ตํ•ด, ๋‹ค์Œ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ธฐํ•„์ฝ” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ „์—ญ์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ๋ฌธ์„œํ™”๋„ ์ œ๋Œ€๋กœ ํ•ด๋†“๊ธฐ๋กœ ๋‹ค์งํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” Custom Exception์„ ํ†ตํ•ด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋ณด๋ ค ํ•œ๋‹ค.

 

๐Ÿ“‘ ์šฐ๋ฆฌ์˜ ํ”„๋กœ์ ํŠธ ์ƒํ™ฉ

API ๋ช…์„ธ๋ฅผ ํ•˜๋ฉฐ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ ์ƒํ™ฉ๋“ค์„ ์ •๋ฆฌํ•˜์˜€๊ณ ,

์˜ˆ์™ธ๋“ค์„ ๋ช‡ ๊ฐ€์ง€ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋‚˜๋ˆด๋‹ค.

AUTH, PAYMENT, SETTLEMENT, TRAVEL, VALIDATION์ด๋ผ๋Š” ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๋‚˜๋ˆด๊ณ 

PAYMENT, SETTLEMENT, TRAVEL ๊ด€๋ จ ์˜ˆ์™ธ๋Š” ์„œ๋น„์Šค ์ •์ฑ…์ƒ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ์ด๋‹ค.

 

๐Ÿ“‘ Custom Exception

Custom Exception ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด๋‹ˆ

๋Œ€์ฒด๋กœ CutomErrorCode, CustomException, CustomExceptionHandler, CustomExceptionResponse

์ด ๋„ค ๊ฐ€์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค.

 

์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ์— ์ด์— ๋Œ€ํ•œ ๊ฐœ๋…์ด ์ž์„ธํžˆ ๋‚˜์™€์žˆ๊ณ , ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

https://velog.io/@rungoat/SpringBoot-Custom-Exception-%EC%B2%98%EB%A6%AC

 

๐Ÿ’ก CustomErrorCode

Errorcode ์ •๋ณด๋ฅผ ๋‹ด์•„ ๊ด€๋ฆฌํ•˜๋Š” enum

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

@Getter
@AllArgsConstructor
public enum ErrorCode {

	USER_NOT_FOUND(HttpStatus.NOT_FOUND, "ACCOUNT-001", "์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
    HAS_EMAIL(HttpStatus.BAD_REQUEST, "ACCOUNT-002", "์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค."),
    INVALID_PASSWORD(HttpStatus.BAD_REQUEST, "ACCOUNT-003", "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
    // ...
    
    private final HttpStatus httpStatus;	// HttpStatus
    private final String code;				// ACCOUNT-001
    private final String message;			// ์„ค๋ช…
}

๐Ÿ’ก CustomException

Exception์ด๋‚˜ RuntimeException์„ ์ƒ์†๋ฐ›๊ณ  CustomErrorCode๋ฅผ ํ•„๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” class

@Getter
@AllArgsConstructor
public class CustomException extends RuntimeException {
    ErrorCode errorCode;
}

๐Ÿ’ก CustomExceptionHandler

CustomException์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ „์—ญ์—์„œ ์ด ์˜ˆ์™ธ๋ฅผ ์žก์•„ ํ•ธ๋“ค๋งํ•ด ์ฃผ๋Š” class

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(CustomException.class)
    protected ResponseEntity<ErrorResponseEntity> handleCustomException(CustomException e){
        return ErrorResponseEntity.toResponseEntity(e.getErrorCode());
    }
}

๐Ÿ’ก CustomExceptionResponse

CustomException์ด ๋ฐœ์ƒํ•ด์„œ CustomExceptionHandler๊ฐ€ ์ด๋ฅผ ๊ฐ์ง€ํ–ˆ์„ ๋•Œ,
์‘๋‹ต์œผ๋กœ ๋‚ด๋ ค์ค„ Response Dto

@Data
@Builder
public class ErrorResponseEntity {
    private int status;
    private String name;
    private String code;
    private String message;

    public static ResponseEntity<ErrorResponseEntity> toResponseEntity(ErrorCode e){
        return ResponseEntity
                .status(e.getHttpStatus())
                .body(ErrorResponseEntity.builder()
                		.status(e.getHttpStatus().value())
                        .name(e.name())
                        .code(e.getCode())
                        .message(e.getMessage())
                        .build());
    }
}

๐Ÿ‘‰ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ErrorCode ๊ด€๋ฆฌํ•˜๊ธฐ

๋ณดํ†ต ErrorCode๋“ค์„ CustomErrorCode๋ผ๋Š” ํ•˜๋‚˜์˜ enum ์•ˆ์—์„œ ๋ชจ๋‘ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ ErrorCode๋ฅผ ๋ถ„๋ฅ˜ํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

 

์šฐ์„  ErrorCode๋ฅผ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ ๋ถ„๋ฅ˜ํ•ด์„œ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„  ํ•œ ๊ฐ€์ง€ ์กฐ๊ฑด์ด ํ•„์š”ํ–ˆ๋‹ค.

 

ErrorCode๋Š” ๋ชจ๋‘ ํ•œ ๊ฐ€์ง€ ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค.

 

๊ทธ ์ด์œ ๋Š” CustomExceptionResponse์—์„œ ErrorResponseEntity class์˜ toResponseEntity ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด

ErrorCode๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ณ  ์žˆ๋‹ค.

 

๋งŒ์•ฝ ErrorCode์˜ ํƒ€์ž…์ด ๋ชจ๋‘ ์ œ๊ฐ๊ฐ์ด๋ฉด, ErrorCode ๋ณ„๋กœ toResponseEntity ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋กœ๋”ฉํ•ด์„œ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

 

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ErrorCode๋ฅผ ์ƒ์†ํ•˜๋Š” AuthErrorCode, PaymentErrorCode ... ๋ฅผ ๋งŒ๋“ค์–ด ํ•˜๋‚˜์˜ toResponseEntity ๋ฉ”์„œ๋“œ๋กœ๋„ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋Ÿผ ์šฐ์„  ErrorCode๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž.

 

์œ„์˜ CustomErrorCode๋Š” enum์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š”๋ฐ

enum์€ ์ด๋ฏธ java.lang.Enum ํด๋ž˜์Šค๋ฅผ ์•”๋ฌต์ ์œผ๋กœ ์ƒ์†ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—,

์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ErrorCode๋“ค๋„ enum์œผ๋กœ ๋งŒ๋“ค๋ฉด,  ErrorCode๋ฅผ ์ƒ์†ํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๋”ฐ๋ผ์„œ ErrorCode๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•˜๊ณ ,

์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ErrorCode๋ฅผ enum์ด ์•„๋‹Œ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด ErrorCode๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ErrorCode๋“ค์—์„œ ์ •์  ์ƒ์ˆ˜ ๊ฐ์ฒด ์ •์˜๋ฅผ ํ†ตํ•ด enum์€ ์•„๋‹ˆ์ง€๋งŒ enum ์ƒ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

 

๐Ÿ’ก ErrorCode

package withbeetravel.exception.error;

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

@Getter
@AllArgsConstructor
public abstract class ErrorCode {

    private final HttpStatus status;
    private final String name;
    private final String code;
    private final String message;
}

๐Ÿ’ก AuthErrorCode

package withbeetravel.exception.error;

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

@Getter
public class AuthErrorCode extends ErrorCode{

    // ์ •์  ์ƒ์ˆ˜ ๊ฐ์ฒด ์ •์˜
    public static final AuthErrorCode AUTHENTICATION_FAILED = new AuthErrorCode(HttpStatus.UNAUTHORIZED, "AUTHENTICATION_FAILED", "AUTH-001", "์‚ฌ์šฉ์ž ์ธ์ฆ ์‹คํŒจ");
    public static final AuthErrorCode ADMIN_PRIVILEGES_REQUIRED = new AuthErrorCode(HttpStatus.FORBIDDEN, "ADMIN_PRIVILEGES_REQUIRED", "AUTH-002", "๊ด€๋ฆฌ์ž ๊ถŒํ•œ ์—†์Œ");
    public static final AuthErrorCode INVALID_CREDENTIALS = new AuthErrorCode(HttpStatus.UNAUTHORIZED, "INVALID_CREDENTIALS", "AUTH-003", "์ž˜๋ชป๋œ ์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ");
    public static final AuthErrorCode EMAIL_NOT_FOUND = new AuthErrorCode(HttpStatus.NOT_FOUND, "EMAIL_NOT_FOUND", "AUTH-004", "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ด๋ฉ”์ผ");
    public static final AuthErrorCode TERMS_NOT_ACCEPTED = new AuthErrorCode(HttpStatus.FORBIDDEN, "TERMS_NOT_ACCEPTED", "AUTH-005", "์„œ๋น„์Šค ์ด์šฉ ์•ฝ๊ด€์— ๋™์˜ํ•˜์ง€ ์•Š์Œ");
    public static final AuthErrorCode EMAIL_ALREADY_EXISTS = new AuthErrorCode(HttpStatus.CONFLICT, "EMAIL_ALREADY_EXISTS", "AUTH-006", "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ");
    public static final AuthErrorCode PASSWORD_POLICY_VIOLATION = new AuthErrorCode(HttpStatus.BAD_REQUEST, "AUTH-007", "PASSWORD_POLICY_VIOLATION", "๋น„๋ฐ€๋ฒˆํ˜ธ ์ •์ฑ… ๋ถˆ์ถฉ์กฑ");
    public static final AuthErrorCode INVALID_EMAIL_FORMAT = new AuthErrorCode(HttpStatus.BAD_REQUEST, "AUTH-008", "INVALID_EMAIL_FORMAT", "์ž˜๋ชป๋œ ์ด๋ฉ”์ผ ํ˜•์‹");
    public static final AuthErrorCode PIN_POLICY_VIOLATION = new AuthErrorCode(HttpStatus.BAD_REQUEST, "PIN_POLICY_VIOLATION", "AUTH-009", "ํ•€๋ฒˆํ˜ธ ์ •์ฑ… ๋ถˆ์ถฉ์กฑ");

    private AuthErrorCode(HttpStatus status, String name, String code, String message) {
        super(status, name, code, message);
    }
}

๐Ÿ’ก PaymentErrorCode

package withbeetravel.exception.error;

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

@Getter
public class PaymentErrorCode extends ErrorCode{

    // ์ •์  ์ƒ์ˆ˜ ๊ฐ์ฒด ์ •์˜
    public static final PaymentErrorCode SHARED_PAYMENT_NOT_FOUND = new PaymentErrorCode(HttpStatus.NOT_FOUND, "SHARED_PAYMENT_NOT_FOUND", "PAYMENT-001", "SHARED PAYMENT ID์— ํ•ด๋‹นํ•˜๋Š” ๊ณต๋™ ๊ฒฐ์ œ ๋‚ด์—ญ์ด ์—†์Œ");
    public static final PaymentErrorCode NO_PERMISSION_TO_MODIFY_SHARED_PAYMENT = new PaymentErrorCode(HttpStatus.FORBIDDEN, "NO_PERMISSION_TO_MODIFY_SHARED_PAYMENT", "PAYMENT-002", "ํ•ด๋‹น ๊ณต๋™ ๊ฒฐ์ œ ๋‚ด์—ญ ์ˆ˜์ • ๊ถŒํ•œ ์—†์Œ");

    private PaymentErrorCode(HttpStatus status, String name, String code, String message) {
        super(status, name, code, message);
    }
}

 

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•จ์œผ๋กœ์จ ErrorCode๋ฅผ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๊ตฌ๋ถ„ํ•˜๋˜,

ํ•œ ๊ฐ€์ง€ ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

 

๐Ÿ‘‰ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ Exception ๊ด€๋ฆฌํ•˜๊ธฐ → CustomException์œผ๋กœ ๋ชจ๋‘ ๊ด€๋ฆฌ

์›๋ž˜๋Š” CustomException๋„ ํ•˜๋‚˜๋กœ ๋ญ‰๋šฑ๊ทธ๋ฆฌ์ง€ ์•Š๊ณ ,

์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๊ด€๋ฆฌํ•˜์—ฌ ์–ด๋Š ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ธ์ง€ ์ง๊ด€์ ์œผ๋กœ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ์ƒ๊ฐ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๋ชจ๋“  Exception์„ ๋งŒ๋“ค์—ˆ๋‹ค.

 

๋˜ํ•œ, ์ด๋ ‡๊ฒŒ ์ƒ๊ฒจ๋‚œ Exception๋“ค์„ ํ•ธ๋“ค๋งํ•  handler ๋ฉ”์„œ๋“œ๋„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.

๐Ÿ’ก AuthException

package withbeetravel.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import withbeetravel.exception.error.AuthErrorCode;

@Getter
@AllArgsConstructor
public class AuthException extends RuntimeException {

    AuthErrorCode errorCode;
}

๐Ÿ’ก PaymentException

package withbeetravel.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import withbeetravel.exception.error.PaymentErrorCode;

@Getter
@AllArgsConstructor
public class PaymentException extends RuntimeException {

    PaymentErrorCode errorCode;
}

๐Ÿ’ก CustomExceptionHandler

package withbeetravel.exception.handler;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import withbeetravel.exception.*;
import withbeetravel.exception.dto.ErrorResponseDto;

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(AuthException.class)
    protected ResponseEntity<ErrorResponseDto> handleAuthException(AuthException e) {
        return ErrorResponseDto.toResponseEntity(e.getErrorCode());
    }

    @ExceptionHandler(PaymentException.class)
    protected ResponseEntity<ErrorResponseDto> handlePaymentException(PaymentException e) {
        return ErrorResponseDto.toResponseEntity(e.getErrorCode());
    }

    @ExceptionHandler(SettlementException.class)
    protected ResponseEntity<ErrorResponseDto> handleSettlementException(SettlementException e) {
        return ErrorResponseDto.toResponseEntity(e.getErrorCode());
    }

    @ExceptionHandler(TravelException.class)
    protected ResponseEntity<ErrorResponseDto> handleTravelException(TravelException e) {
        return ErrorResponseDto.toResponseEntity(e.getErrorCode());
    }

    @ExceptionHandler(ValidationException.class)
    protected ResponseEntity<ErrorResponseDto> handleValidationException(ValidationException e) {
        return ErrorResponseDto.toResponseEntity(e.getErrorCode());
    }
}

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ,

์ฝ”๋“œ ์ค‘๋ณต๋งŒ ์‹ฌํ•ด์ง€๊ณ 

ํ”„๋กœ์ ํŠธ๋ฅผ ๋””๋ฒจ๋กญ์‹œํ‚ค๊ฑฐ๋‚˜ ์œ ์ง€๋ณด์ˆ˜ ๊ณผ์ •์—์„œ ์ƒˆ๋กœ์šด ์นดํ…Œ๊ณ ๋ฆฌ์˜ ์˜ˆ์™ธ๊ฐ€ ์ƒ๊ฒผ์„ ๋•Œ

๋งค๋ฒˆ ์ƒˆ๋กœ์šด Exception๊ณผ handler ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.


์ด๋ฅผ ๊ฐ์ˆ˜ํ•  ๋งŒํผ์˜ ์žฅ์ ์ด ์—†๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค.

 

๋˜ํ•œ ์ด๋ฏธ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ ErrorCode๋ฅผ ๋‚˜๋ˆ ๋†“์•˜๊ธฐ ๋•Œ๋ฌธ์—,

์ด๋ฏธ ์–ด๋Š ๋ถ€๋ถ„์—์„œ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ์ธ์ง€ ํ™•์ธ์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ํ•˜๋‚˜์˜ CustomException์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

๐Ÿ’ก CustomException

package withbeetravel.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import withbeetravel.exception.error.ErrorCode;

@Getter
@AllArgsConstructor
public class CustomException extends RuntimeException{

    ErrorCode errorCode;
}

๐Ÿ’ก CustomExceptionHandler

package withbeetravel.exception.handler;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import withbeetravel.exception.CustomException;
import withbeetravel.exception.dto.ErrorResponseDto;

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(CustomException.class)
    protected ResponseEntity<ErrorResponseDto> handleAuthException(CustomException e) {
        return ErrorResponseDto.toResponseEntity(e.getErrorCode());
    }
}

 

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋‚˜์ค‘์— ์ƒˆ๋กœ์šด ์นดํ…Œ๊ณ ๋ฆฌ์˜ Error๊ฐ€ ์ƒ๊ฒจ๋„

ErrorCode๋ฅผ ์ƒ์†๋ฐ›์€ error code ๊ด€๋ฆฌ ํด๋ž˜์Šค๋งŒ ์ƒ์„ฑํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

๐Ÿ‘‰ CustomeException ์‚ฌ์šฉํ•˜๊ธฐ

CustomException์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์–ด๋–ค Error์ธ์ง€๋งŒ CustomException์˜ ์ƒ์„ฑ์ž์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

new CustomException(AuthErrorCode.AUTHENTICATION_FAILED);
728x90

'Back-end > Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Spring] AOP๋กœ ๊ถŒํ•œ ์ฒ˜๋ฆฌํ•˜๊ธฐ  (0) 2024.11.14
[Spring] Swagger ์„ค์ • ๋ฐ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•  (4) 2024.11.11