Skip to content
Merged
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 @@ -4,10 +4,13 @@
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.util.LinkedHashMap;
Expand Down Expand Up @@ -60,6 +63,38 @@ public ResponseEntity<ErrorResponse> handleMethodNotAllowed(HttpRequestMethodNot
request.getRequestURI(), Map.of()));
}

/**
* 필수 요청 헤더 누락. 예: 미션 API 에 X-Device-Id 를 빠뜨림.
* 클라이언트 잘못이므로 400. 잡지 않으면 catch-all 이 500 으로 만든다.
*/
@ExceptionHandler(MissingRequestHeaderException.class)
public ResponseEntity<ErrorResponse> handleMissingHeader(MissingRequestHeaderException e,
HttpServletRequest request) {
ErrorCode code = ErrorCode.COMMON_ERROR_400_INVALID_INPUT;
return ResponseEntity.status(code.getStatus())
.body(ErrorResponse.of(code, "필수 요청 헤더가 없습니다.", request.getRequestURI(),
Map.of(e.getHeaderName(), "필수 헤더입니다.")));
}

/** 요청 본문을 읽지 못했다. 깨진 JSON, 빈 body, 잘못된 타입 등. 클라이언트 잘못이므로 400. */
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ErrorResponse> handleUnreadableBody(HttpMessageNotReadableException e,
HttpServletRequest request) {
ErrorCode code = ErrorCode.COMMON_ERROR_400_INVALID_INPUT;
return ResponseEntity.status(code.getStatus())
.body(ErrorResponse.of(code, "요청 본문을 읽을 수 없습니다.", request.getRequestURI(), Map.of()));
}

/** 경로/쿼리 파라미터 타입 불일치. 예: Long 자리에 문자열. 클라이언트 잘못이므로 400. */
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ErrorResponse> handleTypeMismatch(MethodArgumentTypeMismatchException e,
HttpServletRequest request) {
ErrorCode code = ErrorCode.COMMON_ERROR_400_INVALID_INPUT;
return ResponseEntity.status(code.getStatus())
.body(ErrorResponse.of(code, code.getMessage(), request.getRequestURI(),
Map.of(e.getName(), "타입이 올바르지 않습니다.")));
}

Comment on lines +66 to +97

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# GlobalExceptionHandler 관련 테스트 존재 여부 확인
rg -n --type=java -C2 'HttpMessageNotReadableException|MethodArgumentTypeMismatchException|MissingRequestHeaderException' --glob '*Test*.java'

Repository: 8th-COKERTHON/server-team1

Length of output: 164


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# 테스트/소스 구조 확인
printf '\n== files ==\n'
git ls-files | rg '(^|/)(src/test|test)/|GlobalExceptionHandler|ErrorResponse|ErrorCode'

printf '\n== handler references ==\n'
rg -n --type=java 'GlobalExceptionHandler|HttpMessageNotReadableException|MethodArgumentTypeMismatchException|MissingRequestHeaderException' src test

printf '\n== test class names ==\n'
rg -n --type=java 'class .*Test|class .*Tests|class .*IT' src/test test

Repository: 8th-COKERTHON/server-team1

Length of output: 2986


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== handler references in tests ==\n'
rg -n --type=java 'GlobalExceptionHandler|HttpMessageNotReadableException|MethodArgumentTypeMismatchException|MissingRequestHeaderException|MockMvc|WebMvcTest|SpringBootTest' src/test

printf '\n== test file contents (short list) ==\n'
for f in $(git ls-files 'src/test/java/**/*.java'); do
  printf '\n--- %s ---\n' "$f"
  sed -n '1,220p' "$f"
done

Repository: 8th-COKERTHON/server-team1

Length of output: 50382


GlobalExceptionHandler의 400 예외 처리 테스트를 추가해 주세요 (GlobalExceptionHandler.java 80-90)
HttpMessageNotReadableExceptionMethodArgumentTypeMismatchException은 서비스 테스트만으로는 커버되지 않으니, @WebMvcTest/MockMvc로 400 응답과 에러 바디를 검증하는 테스트가 있으면 좋겠습니다.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/main/java/com/example/hackathon/global/exception/GlobalExceptionHandler.java`
around lines 66 - 97, GlobalExceptionHandler의 handleUnreadableBody와
handleTypeMismatch에 대한 `@WebMvcTest/MockMvc` 테스트를 추가하세요. 각 예외를 발생시키는 테스트 엔드포인트 또는
요청을 구성하고, HTTP 상태가 400인지와 응답 ErrorResponse의 에러 코드, 메시지, 요청 URI 및 필드 오류 정보가 기대값과
일치하는지 검증하세요.

/**
* 예상 못한 에러.
* 스택트레이스는 로그에만 남기고 응답에는 내부 정보를 노출하지 않는다.
Expand Down
Loading