Skip to content

Commit

Permalink
슬랙 에러 메시지 전송 오류 수정 완료 (#329)
Browse files Browse the repository at this point in the history
* feat(ExceptionHandler): 서버 오류 예외 추가

* fix(Slack): 일부 예외의 파싱 과정에서 발생하는 오류 및 그로 인해 메시지가 정상적으로 보내지지 않던 오류 수정
  • Loading branch information
limehee authored May 4, 2024
1 parent 749be21 commit 0835f4f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand Down Expand Up @@ -124,14 +125,17 @@ private List<LayoutBlock> createErrorBlocks(HttpServletRequest request, Exceptio
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String username = (authentication == null || authentication.getName() == null) ? "anonymous" : authentication.getName();

String errorMessage = e.getMessage() == null ? "No error message provided" : e.getMessage();
String detailedMessage = extractMessageAfterException(errorMessage);
log.error("Server Error: {}", detailedMessage);
return Arrays.asList(
section(section -> section.text(markdownText(":firecracker: *Server Error*"))),
section(section -> section.fields(Arrays.asList(
markdownText("*User:*\n" + username),
markdownText("*Endpoint:*\n[" + httpMethod + "] " + requestUrl)
))),
section(section -> section.text(markdownText("*Error Message:*\n" + e.getMessage().split(":")[1]))),
section(section -> section.text(markdownText("*Stack Trace:*\n```" + Arrays.toString(e.getStackTrace()) + "```")))
section(section -> section.text(markdownText("*Error Message:*\n" + detailedMessage))),
section(section -> section.text(markdownText("*Stack Trace:*\n```" + getStackTraceSummary(e) + "```")))
);
}

Expand Down Expand Up @@ -226,6 +230,19 @@ private List<LayoutBlock> createServerStartBlocks() {
);
}

private String extractMessageAfterException(String message) {
String exceptionIndicator = "Exception:";
int exceptionIndex = message.indexOf(exceptionIndicator);
return exceptionIndex == -1 ? message : message.substring(exceptionIndex + exceptionIndicator.length()).trim();
}

private String getStackTraceSummary(Exception e) {
return Arrays.stream(e.getStackTrace())
.limit(10)
.map(StackTraceElement::toString)
.collect(Collectors.joining("\n"));
}

private String formatMemoryUsage(MemoryUsage memoryUsage) {
long usedMemory = memoryUsage.getUsed() / (1024 * 1024);
long maxMemory = memoryUsage.getMax() / (1024 * 1024);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
Expand Down Expand Up @@ -67,6 +68,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletionException;

@RestControllerAdvice(basePackages = "page.clab.api")
@RequiredArgsConstructor
Expand Down Expand Up @@ -169,11 +171,14 @@ public ErrorResponse conflictException(HttpServletResponse response, Exception e
IllegalStateException.class,
FileUploadFailException.class,
DataIntegrityViolationException.class,
IncorrectResultSizeDataAccessException.class,
ArrayIndexOutOfBoundsException.class,
IOException.class,
WebClientRequestException.class,
TransactionSystemException.class,
SecurityException.class,
CustomOptimisticLockingFailureException.class,
CompletionException.class,
EncryptionException.class,
DecryptionException.class,
Exception.class
Expand Down

0 comments on commit 0835f4f

Please sign in to comment.