Skip to content

Commit

Permalink
feat: 에러를 재현할 수 있는 로그로 수정 (#392)
Browse files Browse the repository at this point in the history
* feat: 로깅에 요청 정보 포함

* feat: 개발 환경 ddl update로 변경
  • Loading branch information
3Juhwan authored Aug 19, 2024
1 parent b167550 commit 4dbc207
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public AdminInterceptor(AuthService authService, AuthenticationExtractor authent

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
log.debug("login request = {}", request.getRequestURI());

String requestURI = request.getRequestURI();

if (requestURI.endsWith("/login")) {
Expand All @@ -46,6 +44,7 @@ private void validateToken(HttpServletRequest request) {
String tokenEventId = authService.findEventIdByToken(token);
String eventId = request.getRequestURI().split("/")[3];
if (!tokenEventId.equals(eventId)) {
log.warn("[행사 접근 불가] Cookie EventId = {}, URL EventId = {}", tokenEventId, eventId);
throw new AuthenticationException(HaengdongErrorCode.FORBIDDEN);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package server.haengdong.exception;

import jakarta.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -15,9 +18,16 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

private static final String LOG_FORMAT = """
[Request URI] {} {}
[Request Body] {}
[Error Message] {}
""";

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<ErrorResponse> authenticationException(AuthenticationException e) {
log.warn(e.getMessage(), e);
public ResponseEntity<ErrorResponse> authenticationException(HttpServletRequest req, AuthenticationException e) {
log.warn(LOG_FORMAT, req.getMethod(), req.getRequestURI(), getRequestBody(req), e.getMessage(), e);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(ErrorResponse.of(e.getErrorCode()));
}
Expand Down Expand Up @@ -55,16 +65,25 @@ public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(Metho
}

@ExceptionHandler(HaengdongException.class)
public ResponseEntity<ErrorResponse> haengdongException(HaengdongException e) {
log.warn(e.getMessage(), e);
public ResponseEntity<ErrorResponse> haengdongException(HttpServletRequest req, HaengdongException e) {
log.warn(LOG_FORMAT, req.getMethod(), req.getRequestURI(), getRequestBody(req), e.getMessage(), e);
return ResponseEntity.badRequest()
.body(ErrorResponse.of(e.getErrorCode()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
log.error(e.getMessage(), e);
public ResponseEntity<ErrorResponse> handleException(HttpServletRequest req, Exception e) {
log.error(LOG_FORMAT, req.getMethod(), req.getRequestURI(), getRequestBody(req), e.getMessage(), e);
return ResponseEntity.internalServerError()
.body(ErrorResponse.of(HaengdongErrorCode.INTERNAL_SERVER_ERROR));
}

private String getRequestBody(HttpServletRequest req) {
try (BufferedReader reader = req.getReader()) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
log.error("Failed to read request body", e);
return "";
}
}
}
2 changes: 1 addition & 1 deletion server/src/main/resources/config

0 comments on commit 4dbc207

Please sign in to comment.