koolbaseDataError function

KoolbaseException koolbaseDataError(
  1. int statusCode,
  2. Map<String, dynamic> body, {
  3. String fallbackMessage = 'Request failed',
})

dependency at its core while koolbaseDataErrorFromResponse offers a convenience wrapper.

Always returns an exception to throw — never null.

Implementation

KoolbaseException koolbaseDataError(
  int statusCode,
  Map<String, dynamic> body, {
  String fallbackMessage = 'Request failed',
}) {
  final code = body['code'] as String?;
  final message = (body['error'] as String?) ?? fallbackMessage;
  final details = body['details'] as Map<String, dynamic>?;

  // ---- code-first ----
  switch (code) {
    case 'unique_violation':
      return KoolbaseConflictException(message, details?['field'] as String?);
    case 'not_found':
    case 'record_not_found':
    case 'collection_not_found':
    case 'vector_not_found':
    case 'vector_field_not_found':
      return KoolbaseNotFoundException(message);
    case 'revision_mismatch':
      return KoolbaseRevisionMismatchException(
        message,
        expectedRevision: (details?['expected_revision'] as num?)?.toInt(),
        currentRevision: (details?['current_revision'] as num?)?.toInt(),
        currentRecord: details?['record'] as Map<String, dynamic>?,
      );
    case 'session_expired':
    case 'invalid_token':
    case 'unauthenticated':
      return KoolbaseUnauthenticatedException(message);
    case 'permission_denied':
      return KoolbasePermissionException(message);
    case 'rate_limit':
      return KoolbaseRateLimitException(message);
    case 'validation_error':
    case 'vector_collection_mismatch':
    case 'unsupported_dimension':
      return KoolbaseValidationException(message);
    case 'vector_dimension_mismatch':
      return KoolbaseVectorDimensionMismatchException(message);
  }

  // ---- status fallback (pre-code servers) ----
  switch (statusCode) {
    case 409:
      return KoolbaseConflictException(message);
    case 404:
      return KoolbaseNotFoundException(message);
    case 401:
      // The status carries the meaning: every 401 from this server reports the
      // same code, so it cannot say whether the session expired, the key was
      // revoked, or the header was malformed. Safe to treat uniformly because a
      // permission failure is 403 — a 401 means the credentials were not
      // accepted, not that this caller may not proceed.
      return KoolbaseUnauthenticatedException(message);
    case 403:
      return KoolbasePermissionException(message);
    case 429:
      return KoolbaseRateLimitException(message);
    case 400:
      return KoolbaseValidationException(message);
  }

  return KoolbaseDataException(message, code: code);
}