TA 역할 Spring 경계 smoke #5
1 changed files with 75 additions and 0 deletions
|
|
@ -0,0 +1,75 @@
|
|||
package com.runtimematrix.exception;
|
||||
|
||||
import com.runtimematrix.dto.ErrorResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public ResponseEntity<ErrorResponse> handleBusinessException(
|
||||
BusinessException ex, HttpServletRequest request) {
|
||||
log.error("Business exception: code={}, message={}", ex.getCode(), ex.getMessage());
|
||||
|
||||
ErrorResponse response = new ErrorResponse(
|
||||
ex.getCode(),
|
||||
ex.getMessage(),
|
||||
ex.getTimestamp().toString(),
|
||||
request.getRequestURI()
|
||||
);
|
||||
|
||||
HttpStatus status = determineHttpStatus(ex);
|
||||
return ResponseEntity.status(status).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ErrorResponse> handleValidationException(
|
||||
MethodArgumentNotValidException ex, HttpServletRequest request) {
|
||||
String message = ex.getBindingResult().getFieldErrors().stream()
|
||||
.map(error -> error.getField() + ": " + error.getDefaultMessage())
|
||||
.reduce((a, b) -> a + "; " + b)
|
||||
.orElse("Validation failed");
|
||||
|
||||
ErrorResponse response = new ErrorResponse(
|
||||
"VALIDATION_ERROR",
|
||||
message,
|
||||
Instant.now().toString(),
|
||||
request.getRequestURI()
|
||||
);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGenericException(
|
||||
Exception ex, HttpServletRequest request) {
|
||||
log.error("Unexpected exception", ex);
|
||||
|
||||
ErrorResponse response = new ErrorResponse(
|
||||
"ERR_INTERNAL",
|
||||
"내부 서버 오류가 발생했습니다",
|
||||
Instant.now().toString(),
|
||||
request.getRequestURI()
|
||||
);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
||||
}
|
||||
|
||||
private HttpStatus determineHttpStatus(BusinessException ex) {
|
||||
String code = ex.getCode();
|
||||
if (code.startsWith("VALIDATION_")) return HttpStatus.BAD_REQUEST;
|
||||
if (code.startsWith("AUTH_")) return HttpStatus.UNAUTHORIZED;
|
||||
if (code.startsWith("CONFLICT_")) return HttpStatus.CONFLICT;
|
||||
if (code.startsWith("ERR_NOT_FOUND")) return HttpStatus.NOT_FOUND;
|
||||
return HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue