Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/backend/globber/exception/MVCGlobalException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package backend.globber.exception;

import java.io.IOException;
import java.util.Locale;
import org.apache.catalina.connector.ClientAbortException;
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
import backend.globber.common.dto.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSourceResolvable;
Expand Down Expand Up @@ -107,7 +111,30 @@ public ResponseEntity<ApiResponse<String>> notFoundHandler(Exception e) {
// 위에서 처리하지 못한 예외는 원본 메시지를 응답에 싣지 않고 로그에만 남긴다
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<String>> unexpectedExceptionHandler(Exception e) {
// 클라이언트가 응답 도중 연결을 끊은 경우: 응답을 쓸 수 없으니 쓰지 않고, 장애가 아니므로 ERROR로 남기지 않는다
if (isClientDisconnect(e)) {
log.debug("클라이언트 연결 끊김: {}", e.getMessage());
return null;
}
log.error("처리하지 못한 예외", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.fail(INTERNAL_ERROR_MESSAGE));
}

private static boolean isClientDisconnect(Throwable e) {
for (Throwable t = e; t != null; t = t.getCause()) {
if (t instanceof AsyncRequestNotUsableException || t instanceof ClientAbortException) {
return true;
}
if (t instanceof IOException && t.getMessage() != null) {
String message = t.getMessage().toLowerCase(Locale.ROOT);
if (message.contains("broken pipe") || message.contains("connection reset by peer")) {
return true;
}
}
if (t.getCause() == t) {
break;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package backend.globber.exception;

import static org.assertj.core.api.Assertions.assertThat;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import java.io.IOException;
import java.util.stream.Stream;
import org.apache.catalina.connector.ClientAbortException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;

class ClientDisconnectHandlingTest {

private final MVCGlobalException handler = new MVCGlobalException();
private final Logger logger = (Logger) LoggerFactory.getLogger(MVCGlobalException.class);
private final ListAppender<ILoggingEvent> appender = new ListAppender<>();

@BeforeEach
void attach() {
appender.start();
logger.addAppender(appender);
}

@AfterEach
void detach() {
logger.detachAppender(appender);
}

static Stream<Exception> clientDisconnects() {
return Stream.of(
new AsyncRequestNotUsableException("ServletOutputStream failed to write: java.io.IOException: Broken pipe",
new IOException("Broken pipe")),
new ClientAbortException(new IOException("Broken pipe")),
new IOException("Broken pipe"),
new IOException("Connection reset by peer"));
}

@ParameterizedTest
@MethodSource("clientDisconnects")
@DisplayName("클라이언트가 연결을 끊어 응답을 못 쓰는 경우는 ERROR·500으로 남기지 않고 응답을 쓰지 않는다")
void clientDisconnect_notLoggedAsError(Exception e) {
// when
var response = handler.unexpectedExceptionHandler(e);

// then
assertThat(response).isNull();
assertThat(appender.list).noneMatch(event -> event.getLevel().isGreaterOrEqual(Level.WARN));
}

@Test
@DisplayName("연결 끊김이 아닌 IOException은 지금처럼 ERROR와 500이다")
void otherIoException_stillError() {
// when
var response = handler.unexpectedExceptionHandler(new IOException("disk full"));

// then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(appender.list).anyMatch(event -> event.getLevel() == Level.ERROR);
}
}
Loading