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

[BE] 에러를 재현할 수 있는 로그로 수정 #392

Merged
merged 2 commits into from
Aug 19, 2024
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
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
Loading