package com.walkguide.exception; import com.walkguide.dto.ApiResponse; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity> handleNotFound(ResourceNotFoundException ex) { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body(ApiResponse.error("NOT_FOUND", ex.getMessage())); } @ExceptionHandler(PairingException.class) public ResponseEntity> handlePairing(PairingException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(ApiResponse.error("PAIRING_ERROR", ex.getMessage())); } @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity> handleValidation(MethodArgumentNotValidException ex) { String msg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage(); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(ApiResponse.error("VALIDATION_ERROR", msg)); } @ExceptionHandler(RuntimeException.class) public ResponseEntity> handleRuntime(RuntimeException ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(ApiResponse.error("INTERNAL_ERROR", ex.getMessage())); } @ExceptionHandler(Exception.class) public ResponseEntity> handleGeneric(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(ApiResponse.error("INTERNAL_ERROR", "Terjadi kesalahan internal")); } }