🖥️ 뭔가뭔가프로젝트/🛠️ 트러블슈팅

[트러블슈팅] 예외 메시지가 null로 출력된 문제..

carrot0911 2025. 5. 28. 10:13

🐛 문제 현상

Kafka Consumer에서 ConflictException이 발생했을 때, 로그 메시지가 null로 출력됐다.

⚠️ 재고 부족으로 메시지 처리 실패: null

→ getMessage()로 예외 메시지를 출력했지만, null이 나왔다.

 

 

🔍 원인 분석

✅ 기존 코드 구조

public class ConflictException extends CustomRuntimeException {
    public ConflictException(ErrorCode errorCode) {
        super(errorCode); // message는 전달하지 않음
    }
}
@Getter
@RequiredArgsConstructor
public class CustomRuntimeException extends RuntimeException {
    private final ErrorCode errorCode;
}
  • CustomRuntimeException에서 RuntimeException의 생성자에 메시지를 전달하지 않았다.
  • 따라서 super()는 기본 생성자가 호출되고, RuntimeException 내부의 detailMessage 필드는 null이 된다.
  • 그 결과 getMessage()는 null을 반환한다.

 

 

🔍 해결 방법

RuntimeException의 메시지를 명시적으로 설정하도록 수정해야 한다.

🔧 개선 방법 1: super(errorCode.getMessage()) 사용

public class CustomRuntimeException extends RuntimeException {
    private final ErrorCode errorCode;

    public CustomRuntimeException(ErrorCode errorCode) {
        super(errorCode.getMessage()); // 메시지를 RuntimeException에게 전달
        this.errorCode = errorCode;
    }
}

🔧 개선 방법 2: getMessage() 오버라이드

@Override
public String getMessage() {
    return errorCode.getMessage();
}

→ 두 방법 중 하나만 적용해도 getMessage()에서 null이 아닌 실제 메시지가 출력된다.

 

 

✅ 개선 결과

예외 발생 시 다음과 같이 메시지가 정상적으로 출력되었다.

⚠️ 재고 부족으로 메시지 처리 실패: 해당 상품의 재고가 부족합니다.

 

 

💡 트러블슈팅 요약

  • 문제 → getMessage() 결과가 null로 출력되었다.
  • 원인 → RuntimeException 생성자에 메시지를 전달하지 않았다.
  • 해결 → super(errorCode.getMessage()) 또는 getMessage() 오버라이드
  • 결과 → Kafka 로그에 예외 메시지가 정상적으로 출력되었다.