Skip to content

Comments

INTERNAL: stacktrace all exceptions#840

Merged
jhpark816 merged 1 commit intonaver:developfrom
oliviarla:composite
Nov 25, 2024
Merged

INTERNAL: stacktrace all exceptions#840
jhpark816 merged 1 commit intonaver:developfrom
oliviarla:composite

Conversation

@oliviarla
Copy link
Collaborator

@oliviarla oliviarla commented Nov 20, 2024

🔗 Related Issue

⌨️ What I did

  • CompositeException 객체의 printStackTrace() 메서드를 통해 예외에 대한 스택을 확인할 때, 아래와 같이 각각의 예외들에 대한 stacktrace가 찍히도록 합니다.

@oliviarla oliviarla requested a review from uhm0311 November 20, 2024 10:48
@uhm0311
Copy link
Collaborator

uhm0311 commented Nov 20, 2024

아래와 같이 단순하게 하면 어떻게 되나요?

@Override
public synchronized Throwable getCause() {
  return !exceptions.isEmpty() ? exceptions.get(0) : null;
}

@Override
public StackTraceElement[] getStackTrace() {
  return exceptions.stream()
          .map(Throwable::getStackTrace)
          .flatMap(Arrays::stream)
          .toArray(StackTraceElement[]::new);
}

@Override
public void printStackTrace(PrintStream s) {
  exceptions.forEach((e) -> e.printStackTrace(s));
}

@Override
public void printStackTrace(PrintWriter s) {
  exceptions.forEach((e) -> e.printStackTrace(s));
}

@jhpark816
Copy link
Collaborator

@oliviarla
기존에는 stacktrace가 어떻게 출력되었나요?

@oliviarla
Copy link
Collaborator Author

@uhm0311
아래와 같이 스택트레이스가 찍히게 됩니다. 현재 구현과 다른 점은 getCause() 메서드 호출 시에 반환되는 Throwable에 전체 예외에 대한 메시지를 담을지 말지 입니다. 저는 getCause 메서드 호출 시에 전체 예외에 대한 메시지가 담긴 throwable을 반환하는 것이 더 좋을 것 같습니다.

OperationException: msg1
    at net.spy.memcached.internal.CompositeExceptionTest.printStackTraceOfAllExceptions(CompositeExceptionTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...
OperationException: msg2
    at net.spy.memcached.internal.CompositeExceptionTest.printStackTraceOfAllExceptions(CompositeExceptionTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...
OperationException: msg3
    at net.spy.memcached.internal.CompositeExceptionTest.throwError2(CompositeExceptionTest.java:34)
    at net.spy.memcached.internal.CompositeExceptionTest.throwError1(CompositeExceptionTest.java:31)
    at net.spy.memcached.internal.CompositeExceptionTest.printStackTraceOfAllExceptions(CompositeExceptionTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...

@jhpark816

기존에는 내부에 담긴 Exception들의 스택트레이스가 없이 CompositeException 자체의 스택트레이스만 반환되었습니다.

net.spy.memcached.internal.CompositeException: Multiple exceptions (3) reported: msg1, msg2, msg3
	at net.spy.memcached.internal.CompositeExceptionTest.printStackTraceOfAllExceptions(CompositeExceptionTest.java:21)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:728)
        ...

@oliviarla oliviarla self-assigned this Nov 21, 2024
Copy link
Collaborator

@uhm0311 uhm0311 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2차, 질문입니다.

@oliviarla oliviarla force-pushed the composite branch 3 times, most recently from 0b102a0 to cce2682 Compare November 21, 2024 08:38
Copy link
Collaborator

@uhm0311 uhm0311 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3차 리뷰 의견입니다.

exceptions.add(new OperationException(OperationErrorType.SERVER, "msg1"));
exceptions.add(new OperationException(OperationErrorType.CLIENT, "msg2"));

logger.error("failed to get", new CompositeException(exceptions));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 로거는 자바 클라이언트 내부에 구현된 로거를 사용할 것입니다.
log4j 의존성은 이미 포함되어 있으니 log4j의 로거도 한 번 사용해봅시다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log4j 사용하는 테스트도 추가했습니다.

Comment on lines 18 to 20
super(ExceptionMessageFactory.createCompositeMessage(exceptions));
if (exceptions.size() > 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사이에 Empty Line 추가

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line 같은 경우에는 정해져있는 룰이 있었나요?
가독성 좋아보이게 제 나름대로 수정해두었습니다.

@jhpark816 jhpark816 merged commit 381b25a into naver:develop Nov 25, 2024
@oliviarla oliviarla deleted the composite branch April 29, 2025 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants