diff --git a/src/main/java/company/specialsource/config/UploadErrorAdvice.java b/src/main/java/company/specialsource/config/UploadErrorAdvice.java new file mode 100644 index 0000000..686757a --- /dev/null +++ b/src/main/java/company/specialsource/config/UploadErrorAdvice.java @@ -0,0 +1,24 @@ +package company.specialsource.config; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.ErrorResponse; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.Map; + +// 업로드 500 원인 찾기용: 컨트롤러 진입 전(멀티파트 해석 등)에 난 오류도 원인을 응답에 담아 화면까지 전달 +// ErrorResponse(400·404 같은 스프링 표준 오류)는 원래대로 두고, 그 밖의 예외만 +@RestControllerAdvice +public class UploadErrorAdvice { + + @ExceptionHandler(Exception.class) + public ResponseEntity handle(Exception e) throws Exception { + if (e instanceof ErrorResponse) throw e; + e.printStackTrace(); + Throwable root = e; + while (root.getCause() != null && root.getCause() != root) root = root.getCause(); + String msg = root == e ? e.toString() : e + " ← " + root; + return ResponseEntity.status(500).body(Map.of("error", "서버 처리 실패: " + msg)); + } +}