Files
project-auth-server/build/reports/jacoco/aggregate/html/com.project.auth.config.web/ApiErrorController.java.html
T

115 lines
8.2 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ApiErrorController.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">project-auth-server</a> &gt; <a href="index.source.html" class="el_package">com.project.auth.config.web</a> &gt; <span class="el_source">ApiErrorController.java</span></div><h1>ApiErrorController.java</h1><pre class="source lang-java linenums">package com.project.auth.config.web;
import com.project.auth.application.support.exception.ClientFacingErrorCode;
import com.project.auth.application.support.exception.CommonErrorCode;
import com.project.auth.application.support.logging.LogSanitizer;
import com.project.auth.presentation.support.exception.PresentationErrorCode;
import com.project.auth.presentation.support.response.ApiResult;
import com.project.auth.presentation.support.response.ApiResultFactory;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.webmvc.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;
/**
* {@code @RestControllerAdvice}를 우회하는 요청(필터에서 던져진 예외,
* {@code response.sendError(...)} 호출, 컨테이너 레벨 라우팅 실패, 다른 핸들러 내부의
* 이중 폴트 등)에 대한 최후의 에러 렌더러.
*
* 컨테이너가 결정한 상태 코드는 그대로 보존하고, 본문만 {@link ApiResult} 형태로 정규화한다.
* 5xx는 내부 분류 노출을 막기 위해 {@code COMMON-999}로 정규화하고, 4xx는
* {@link PresentationErrorCode} 기반 코드로 매핑하여 클라이언트 SDK가
* &quot;요청이 잘못됐다&quot;&quot;서버가 깨졌다&quot;를 구분할 수 있게 한다.
*
* 의존성 정책: 이 컨트롤러는 Spring Boot의 ErrorAttributes를 사용하지 않는다.
* 필요한 정보(상태 코드, 원인 예외, 원래 요청 경로)는 모두 서블릿 표준
* RequestDispatcher.ERROR_* attribute로부터 직접 추출한다. 이렇게 두면
* ErrorAttributeOptions의 변경(예: 스택트레이스 포함)이 이 컨트롤러가 노출하는 정보를
* 본의 아니게 확장하지 못하며, 빈 의존 그래프도 작아진다.
*/
@RestController
class ApiErrorController implements ErrorController {
<span class="fc" id="L40"> private static final Logger log = LoggerFactory.getLogger(ApiErrorController.class);</span>
private final ApiResultFactory apiResultFactory;
<span class="fc" id="L44"> ApiErrorController(ApiResultFactory apiResultFactory) {</span>
<span class="fc" id="L45"> this.apiResultFactory = Objects.requireNonNull(apiResultFactory, &quot;apiResultFactory must not be null&quot;);</span>
<span class="fc" id="L46"> }</span>
@RequestMapping(&quot;/error&quot;)
ResponseEntity&lt;ApiResult&lt;Void&gt;&gt; error(HttpServletRequest request) {
<span class="fc" id="L50"> int rawStatus = resolveStatusCode(request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE));</span>
<span class="fc" id="L51"> HttpStatus httpStatus = safeStatus(rawStatus);</span>
<span class="fc" id="L52"> Throwable error = resolveError(request);</span>
<span class="fc" id="L53"> String requestPath = LogSanitizer.requestPath(originalRequestPath(request));</span>
<span class="fc" id="L54"> String method = request.getMethod();</span>
<span class="pc bpc" id="L56" title="1 of 4 branches missed."> if (httpStatus.is5xxServerError() &amp;&amp; error != null) {</span>
<span class="nc" id="L57"> log.error(&quot;Unhandled exception. method={} requestPath={} status={}&quot;,</span>
<span class="nc" id="L58"> method, requestPath, httpStatus.value(), error);</span>
<span class="fc bfc" id="L59" title="All 2 branches covered."> } else if (httpStatus.is5xxServerError()) {</span>
<span class="fc" id="L60"> log.error(&quot;Unhandled error response. method={} requestPath={} status={}&quot;,</span>
<span class="fc" id="L61"> method, requestPath, httpStatus.value());</span>
} else {
<span class="fc" id="L63"> log.warn(&quot;Unhandled error response. method={} requestPath={} status={}&quot;,</span>
<span class="fc" id="L64"> method, requestPath, httpStatus.value());</span>
}
<span class="fc" id="L67"> ClientFacingErrorCode errorCode = classify(httpStatus);</span>
<span class="fc" id="L69"> return ResponseEntity.status(httpStatus)</span>
<span class="fc" id="L70"> .body(apiResultFactory.failure(errorCode.code(), errorCode.message()));</span>
}
private static ClientFacingErrorCode classify(HttpStatus status) {
<span class="fc bfc" id="L74" title="All 2 branches covered."> if (status.is5xxServerError()) {</span>
<span class="fc" id="L75"> return CommonErrorCode.INTERNAL_SERVER_ERROR;</span>
}
<span class="pc bpc" id="L77" title="3 of 6 branches missed."> return switch (status) {</span>
<span class="fc" id="L78"> case NOT_FOUND -&gt; PresentationErrorCode.RESOURCE_NOT_FOUND;</span>
<span class="fc" id="L79"> case METHOD_NOT_ALLOWED -&gt; PresentationErrorCode.METHOD_NOT_ALLOWED;</span>
<span class="nc" id="L80"> case UNSUPPORTED_MEDIA_TYPE -&gt; PresentationErrorCode.UNSUPPORTED_MEDIA_TYPE;</span>
<span class="nc" id="L81"> case NOT_ACCEPTABLE -&gt; PresentationErrorCode.NOT_ACCEPTABLE;</span>
<span class="nc" id="L82"> case CONTENT_TOO_LARGE -&gt; PresentationErrorCode.PAYLOAD_TOO_LARGE;</span>
<span class="fc" id="L83"> default -&gt; PresentationErrorCode.UNHANDLED_CLIENT_ERROR;</span>
};
}
private static HttpStatus safeStatus(int rawStatus) {
try {
<span class="fc" id="L89"> return HttpStatus.valueOf(rawStatus);</span>
<span class="fc" id="L90"> } catch (IllegalArgumentException ignored) {</span>
<span class="fc" id="L91"> return HttpStatus.INTERNAL_SERVER_ERROR;</span>
}
}
private static int resolveStatusCode(Object status) {
<span class="fc bfc" id="L96" title="All 2 branches covered."> if (status instanceof Integer value) {</span>
<span class="fc" id="L97"> return value;</span>
}
<span class="fc" id="L99"> return HttpStatus.INTERNAL_SERVER_ERROR.value();</span>
}
private static String originalRequestPath(HttpServletRequest request) {
<span class="fc" id="L103"> Object path = request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);</span>
<span class="pc bpc" id="L104" title="1 of 4 branches missed."> if (path instanceof String requestUri &amp;&amp; !requestUri.isBlank()) {</span>
<span class="fc" id="L105"> return requestUri;</span>
}
<span class="fc" id="L107"> return request.getRequestURI();</span>
}
private static Throwable resolveError(HttpServletRequest request) {
<span class="fc" id="L111"> Object exception = request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);</span>
<span class="pc bpc" id="L112" title="1 of 2 branches missed."> return exception instanceof Throwable throwable ? throwable : null;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.14.202510111229</span></div></body></html>