Skip to content

[refactor][queue-service] Redis 인프라 예외를 도메인 예외로 변환 #12

@rlaxxwls13

Description

@rlaxxwls13

📌 개요

현재 RedisQueueTokenRepository 의 메서드들이 Spring Data Redis 의 예외 (DataAccessException, RedisConnectionFailureException 등) 를 그대로 상위 계층으로 전파한다. DDD 원칙상 인프라 구현 세부사항은 도메인 경계에서 변환되어야 한다.

이로 인해 Service / Controller 가 Spring Redis 구체 클래스에 결합되며, 향후 저장소 교체 시 호출자 코드 변경이 필요하다.

🎯 목표

  • 인프라 예외 → 도메인 예외 변환
  • 상위 계층의 인프라 결합 제거
  • DDD 원칙 준수 (인프라 세부사항 차단)

🧩 리팩토링 범위

  • 클래스 / 패키지 구조 개선
  • 메서드 분리 / 네이밍 개선
  • 중복 로직 제거
  • 트랜잭션 / 예외 처리 정리

🏗️ 변경 설계

변경 전 구조

@Override
public void enqueue(QueueToken token) {
    redisTemplate.opsForValue().setIfAbsent(...);   // RedisException 그대로 전파
    redisTemplate.execute(...);                      // RedisException 그대로 전파
}

→ Service 가 Spring Redis 의 DataAccessException 등을 직접 처리해야 함.

변경 후 구조

1. 도메인 예외 추가

public class QueueStorageException extends BusinessException {
    public QueueStorageException(Throwable cause) {
        super(QueueErrorCode.STORAGE_ERROR, cause);
    }
}

2. ErrorCode 추가

STORAGE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "대기열 저장소 처리 중 오류가 발생했습니다")

3-A. 핵심 메서드 try-catch (간단)

@Override
public void enqueue(QueueToken token) {
    try {
        // 기존 로직
    } catch (DataAccessException e) {
        throw new QueueStorageException(e);
    }
}

3-B. 또는 AOP 일괄 처리

@Aspect
@Component
public class RedisExceptionAspect {
    @Around("execution(* com.firstticket.queueservice.infrastructure.redis..*(..))")
    public Object handleRedisException(ProceedingJoinPoint pjp) throws Throwable {
        try {
            return pjp.proceed();
        } catch (DataAccessException e) {
            throw new QueueStorageException(e);
        }
    }
}

영향 받는 기능

  • RedisQueueTokenRepository 의 모든 메서드 (enqueue, findById, findByUserIdAndProgramId, delete, findRank, findAdmissionCandidates)
  • Service / Controller 의 예외 처리 로직 (Spring Redis → 도메인 예외)

⚠️ 주의 사항

  • 기능 동작은 기존과 동일 (예외 클래스만 변경)
  • API 스펙 변경 없음 (응답 메시지 / 코드는 동일)
  • DuplicateTokenException 등 기존 도메인 예외는 그대로 유지 (래핑 X)
  • AOP 도입 시 advice 우선순위 검토

🧪 검증 방법

  • 기존 테스트 통과 여부 확인
  • Redis 장애 시뮬레이션 테스트 (Testcontainers Redis 중지 → QueueStorageException 검증)
  • 기존 도메인 예외 (DuplicateTokenException) 가 래핑되지 않는지 확인

Metadata

Metadata

Assignees

Labels

No fields configured for Feature.

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions