TA 역할 Spring 경계 smoke #7
1 changed files with 37 additions and 0 deletions
|
|
@ -0,0 +1,37 @@
|
|||
package com.klaroworks.runtime.exception;
|
||||
|
||||
import com.klaroworks.runtime.dto.ApiResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public ResponseEntity<ApiResponse<Void>> handleBusinessException(BusinessException ex) {
|
||||
log.warn("Business exception: {}", ex.getErrorCode().getCode());
|
||||
return ResponseEntity.status(ex.getErrorCode().getHttpStatus())
|
||||
.body(ApiResponse.error(ex.getErrorCode()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ApiResponse<Void>> handleValidationException(MethodArgumentNotValidException ex) {
|
||||
var errors = ex.getBindingResult().getFieldErrors().stream()
|
||||
.collect(java.util.stream.Collectors.toMap(
|
||||
e -> e.getField(), e -> e.getDefaultMessage()));
|
||||
log.warn("Validation failed: {}", errors);
|
||||
return ResponseEntity.badRequest()
|
||||
.body(ApiResponse.error(ErrorCode.INVALID_REQUEST, errors));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ApiResponse<Void>> handleGenericException(Exception ex) {
|
||||
log.error("Unexpected error", ex);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(ApiResponse.error(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue