All files / packages/gemini-core/src/utils quotaErrorDetection.ts

13.33% Statements 4/30
0% Branches 0/31
0% Functions 0/5
13.33% Lines 4/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                      6x                   6x                 6x                                                                                                 6x                            
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
 
export interface StructuredError {
  message: string;
  status?: number;
}
export interface ApiError {
  error: {
    code: number;
    message: string;
    status: string;
    details: unknown[];
  };
}
 
export function isApiError(error: unknown): error is ApiError {
  return (
    typeof error === 'object' &&
    error !== null &&
    'error' in error &&
    typeof (error as ApiError).error === 'object' &&
    'message' in (error as ApiError).error
  );
}
 
export function isStructuredError(error: unknown): error is StructuredError {
  return (
    typeof error === 'object' &&
    error !== null &&
    'message' in error &&
    typeof (error as StructuredError).message === 'string'
  );
}
 
export function isProQuotaExceededError(error: unknown): boolean {
  // Check for Pro quota exceeded errors by looking for the specific pattern
  // This will match patterns like:
  // - "Quota exceeded for quota metric 'Gemini 2.5 Pro Requests'"
  // - "Quota exceeded for quota metric 'Gemini 2.5-preview Pro Requests'"
  // We use string methods instead of regex to avoid ReDoS vulnerabilities
 
  const checkMessage = (message: string): boolean =>
    message.includes("Quota exceeded for quota metric 'Gemini") &&
    message.includes("Pro Requests'");
 
  Iif (typeof error === 'string') {
    return checkMessage(error);
  }
 
  Iif (isStructuredError(error)) {
    return checkMessage(error.message);
  }
 
  Iif (isApiError(error)) {
    return checkMessage(error.error.message);
  }
 
  // Check if it's a Gaxios error with response data
  Iif (error && typeof error === 'object' && 'response' in error) {
    const gaxiosError = error as {
      response?: {
        data?: unknown;
      };
    };
    Iif (gaxiosError.response && gaxiosError.response.data) {
      Iif (typeof gaxiosError.response.data === 'string') {
        return checkMessage(gaxiosError.response.data);
      }
      Iif (
        typeof gaxiosError.response.data === 'object' &&
        gaxiosError.response.data !== null &&
        'error' in gaxiosError.response.data
      ) {
        const errorData = gaxiosError.response.data as {
          error?: { message?: string };
        };
        return checkMessage(errorData.error?.message || '');
      }
    }
  }
  return false;
}
 
export function isGenericQuotaExceededError(error: unknown): boolean {
  Iif (typeof error === 'string') {
    return error.includes('Quota exceeded for quota metric');
  }
 
  Iif (isStructuredError(error)) {
    return error.message.includes('Quota exceeded for quota metric');
  }
 
  Iif (isApiError(error)) {
    return error.error.message.includes('Quota exceeded for quota metric');
  }
 
  return false;
}