Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 배치 공통 예외 처리 추출 #79

Merged
merged 1 commit into from
Aug 6, 2023

Conversation

sooyoungh
Copy link
Member

@sooyoungh sooyoungh commented Aug 4, 2023

구현 사항

  • 배치 실행 시, 공통 예외 처리 로직(try-catch문) BatchExceptionUtil에 추출

참고 사항

  • 기존에 각 BatchService단에서 중간 메소드들이 throws로 모든 예외를 최종 메소드(getAllProducts())로 던져준 방식에서
  • 다음처럼 분리해서 처리했습니다.
    • Document 페치 시 예외(IOException)
    • json 파싱 시 예외(JsonProcessingException)
    • 그외의 배치 시 예외들
    • 각각 BatchExceptionUtil에서 공통 try-catch문 추출

기존

        try {
            return getProducts();
        } catch (IllegalArgumentException e) {
            throw new PyonsnalcolorBatchException(INVALID_ACCESS, e);
        } catch (SocketTimeoutException e) {
            throw new PyonsnalcolorBatchException(TIME_OUT, e);
        } catch (IOException e) {
            throw new PyonsnalcolorBatchException(IO_EXCEPTION, e);
        } catch (Exception e) {
            throw new PyonsnalcolorBatchException(BATCH_UNAVAILABLE, e);
        }

예외처리 추출 후

  • BatchExceptionUtil
    public static <T> T handleException(Supplier<T> supplier) {
        try {
            return supplier.get();
        } catch (IllegalArgumentException e) {
            throw new PyonsnalcolorBatchException(INVALID_ACCESS, e);
        } catch (Exception e) {
            throw new PyonsnalcolorBatchException(BATCH_UNAVAILABLE, e);
        }
    }

    public static Document getDocumentByUrlWithTimeout(String url, int timeOut) {
        try {
            return Jsoup.connect(url).timeout(timeOut).get();
        } catch (IOException e) {
            throw new PyonsnalcolorBatchException(IO_EXCEPTION, e);
        }
    }
  • 배치 서비스단
protected List<BasePbProduct> getAllProducts() {
        return BatchExceptionUtil.handleException(this::getProductsByCategoryAll);
    }
...
    Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(pagedCuEventUrl, TIMEOUT);

- Document 페치 시 예외(IOException)
- json 파싱 시 예외(JsonProcessingException)
- 그외의 배치 시 예외들
각각 BatchExceptionUtil에서 공통 try-catch문 추출
@sooyoungh sooyoungh self-assigned this Aug 4, 2023
@sooyoungh sooyoungh merged commit 7affd3d into develop Aug 6, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant