@google/gemini-cli-core 0.9.0-preview.3 → 0.9.0-preview.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/google-gemini-cli-core-0.9.0-preview.4.tgz +0 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/src/generated/git-commit.d.ts +2 -2
- package/dist/src/generated/git-commit.js +2 -2
- package/dist/src/utils/errorParsing.d.ts +1 -1
- package/dist/src/utils/errorParsing.js +33 -5
- package/dist/src/utils/errorParsing.js.map +1 -1
- package/dist/src/utils/errorParsing.test.js +88 -0
- package/dist/src/utils/errorParsing.test.js.map +1 -1
- package/dist/src/utils/flashFallback.test.js +45 -26
- package/dist/src/utils/flashFallback.test.js.map +1 -1
- package/dist/src/utils/quotaErrorDetection.d.ts +2 -0
- package/dist/src/utils/quotaErrorDetection.js +46 -0
- package/dist/src/utils/quotaErrorDetection.js.map +1 -1
- package/dist/src/utils/retry.js +146 -29
- package/dist/src/utils/retry.js.map +1 -1
- package/dist/src/utils/retry.test.js +110 -31
- package/dist/src/utils/retry.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/dist/google-gemini-cli-core-0.9.0-preview.2.tgz +0 -0
- package/dist/src/utils/googleErrors.d.ts +0 -104
- package/dist/src/utils/googleErrors.js +0 -108
- package/dist/src/utils/googleErrors.js.map +0 -1
- package/dist/src/utils/googleErrors.test.d.ts +0 -6
- package/dist/src/utils/googleErrors.test.js +0 -212
- package/dist/src/utils/googleErrors.test.js.map +0 -1
- package/dist/src/utils/googleQuotaErrors.d.ts +0 -35
- package/dist/src/utils/googleQuotaErrors.js +0 -108
- package/dist/src/utils/googleQuotaErrors.js.map +0 -1
- package/dist/src/utils/googleQuotaErrors.test.d.ts +0 -6
- package/dist/src/utils/googleQuotaErrors.test.js +0 -189
- package/dist/src/utils/googleQuotaErrors.test.js.map +0 -1
package/package.json
CHANGED
|
Binary file
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2025 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* @fileoverview
|
|
8
|
-
* This file contains types and functions for parsing structured Google API errors.
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Based on google/rpc/error_details.proto
|
|
12
|
-
*/
|
|
13
|
-
export interface ErrorInfo {
|
|
14
|
-
'@type': 'type.googleapis.com/google.rpc.ErrorInfo';
|
|
15
|
-
reason: string;
|
|
16
|
-
domain: string;
|
|
17
|
-
metadata: {
|
|
18
|
-
[key: string]: string;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
export interface RetryInfo {
|
|
22
|
-
'@type': 'type.googleapis.com/google.rpc.RetryInfo';
|
|
23
|
-
retryDelay: string;
|
|
24
|
-
}
|
|
25
|
-
export interface DebugInfo {
|
|
26
|
-
'@type': 'type.googleapis.com/google.rpc.DebugInfo';
|
|
27
|
-
stackEntries: string[];
|
|
28
|
-
detail: string;
|
|
29
|
-
}
|
|
30
|
-
export interface QuotaFailure {
|
|
31
|
-
'@type': 'type.googleapis.com/google.rpc.QuotaFailure';
|
|
32
|
-
violations: Array<{
|
|
33
|
-
subject: string;
|
|
34
|
-
description: string;
|
|
35
|
-
apiService?: string;
|
|
36
|
-
quotaMetric?: string;
|
|
37
|
-
quotaId?: string;
|
|
38
|
-
quotaDimensions?: {
|
|
39
|
-
[key: string]: string;
|
|
40
|
-
};
|
|
41
|
-
quotaValue?: number;
|
|
42
|
-
futureQuotaValue?: number;
|
|
43
|
-
}>;
|
|
44
|
-
}
|
|
45
|
-
export interface PreconditionFailure {
|
|
46
|
-
'@type': 'type.googleapis.com/google.rpc.PreconditionFailure';
|
|
47
|
-
violations: Array<{
|
|
48
|
-
type: string;
|
|
49
|
-
subject: string;
|
|
50
|
-
description: string;
|
|
51
|
-
}>;
|
|
52
|
-
}
|
|
53
|
-
export interface LocalizedMessage {
|
|
54
|
-
'@type': 'type.googleapis.com/google.rpc.LocalizedMessage';
|
|
55
|
-
locale: string;
|
|
56
|
-
message: string;
|
|
57
|
-
}
|
|
58
|
-
export interface BadRequest {
|
|
59
|
-
'@type': 'type.googleapis.com/google.rpc.BadRequest';
|
|
60
|
-
fieldViolations: Array<{
|
|
61
|
-
field: string;
|
|
62
|
-
description: string;
|
|
63
|
-
reason?: string;
|
|
64
|
-
localizedMessage?: LocalizedMessage;
|
|
65
|
-
}>;
|
|
66
|
-
}
|
|
67
|
-
export interface RequestInfo {
|
|
68
|
-
'@type': 'type.googleapis.com/google.rpc.RequestInfo';
|
|
69
|
-
requestId: string;
|
|
70
|
-
servingData: string;
|
|
71
|
-
}
|
|
72
|
-
export interface ResourceInfo {
|
|
73
|
-
'@type': 'type.googleapis.com/google.rpc.ResourceInfo';
|
|
74
|
-
resourceType: string;
|
|
75
|
-
resourceName: string;
|
|
76
|
-
owner: string;
|
|
77
|
-
description: string;
|
|
78
|
-
}
|
|
79
|
-
export interface Help {
|
|
80
|
-
'@type': 'type.googleapis.com/google.rpc.Help';
|
|
81
|
-
links: Array<{
|
|
82
|
-
description: string;
|
|
83
|
-
url: string;
|
|
84
|
-
}>;
|
|
85
|
-
}
|
|
86
|
-
export type GoogleApiErrorDetail = ErrorInfo | RetryInfo | DebugInfo | QuotaFailure | PreconditionFailure | BadRequest | RequestInfo | ResourceInfo | Help | LocalizedMessage;
|
|
87
|
-
export interface GoogleApiError {
|
|
88
|
-
code: number;
|
|
89
|
-
message: string;
|
|
90
|
-
details: GoogleApiErrorDetail[];
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Parses an error object to check if it's a structured Google API error
|
|
94
|
-
* and extracts all details.
|
|
95
|
-
*
|
|
96
|
-
* This function can handle two formats:
|
|
97
|
-
* 1. Standard Google API errors where `details` is a top-level field.
|
|
98
|
-
* 2. Errors where the entire structured error object is stringified inside
|
|
99
|
-
* the `message` field of a wrapper error.
|
|
100
|
-
*
|
|
101
|
-
* @param error The error object to inspect.
|
|
102
|
-
* @returns A GoogleApiError object if the error matches, otherwise null.
|
|
103
|
-
*/
|
|
104
|
-
export declare function parseGoogleApiError(error: unknown): GoogleApiError | null;
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2025 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Parses an error object to check if it's a structured Google API error
|
|
8
|
-
* and extracts all details.
|
|
9
|
-
*
|
|
10
|
-
* This function can handle two formats:
|
|
11
|
-
* 1. Standard Google API errors where `details` is a top-level field.
|
|
12
|
-
* 2. Errors where the entire structured error object is stringified inside
|
|
13
|
-
* the `message` field of a wrapper error.
|
|
14
|
-
*
|
|
15
|
-
* @param error The error object to inspect.
|
|
16
|
-
* @returns A GoogleApiError object if the error matches, otherwise null.
|
|
17
|
-
*/
|
|
18
|
-
export function parseGoogleApiError(error) {
|
|
19
|
-
if (!error) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
let errorObj = error;
|
|
23
|
-
// If error is a string, try to parse it.
|
|
24
|
-
if (typeof errorObj === 'string') {
|
|
25
|
-
try {
|
|
26
|
-
errorObj = JSON.parse(errorObj);
|
|
27
|
-
}
|
|
28
|
-
catch (_) {
|
|
29
|
-
// Not a JSON string, can't parse.
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (typeof errorObj !== 'object' || errorObj === null) {
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
const gaxiosError = errorObj;
|
|
37
|
-
let outerError;
|
|
38
|
-
if (gaxiosError.response?.data) {
|
|
39
|
-
if (typeof gaxiosError.response.data === 'string') {
|
|
40
|
-
try {
|
|
41
|
-
const parsedData = JSON.parse(gaxiosError.response.data);
|
|
42
|
-
if (parsedData.error) {
|
|
43
|
-
outerError = parsedData.error;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
catch (_) {
|
|
47
|
-
// Not a JSON string, or doesn't contain .error
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else if (typeof gaxiosError.response.data === 'object' &&
|
|
51
|
-
gaxiosError.response.data !== null) {
|
|
52
|
-
outerError = gaxiosError.response.data.error;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const responseStatus = gaxiosError.response?.status;
|
|
56
|
-
if (!outerError) {
|
|
57
|
-
// If the gaxios structure isn't there, check for a top-level `error` property.
|
|
58
|
-
if (gaxiosError.error) {
|
|
59
|
-
outerError = gaxiosError.error;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
let currentError = outerError;
|
|
66
|
-
let depth = 0;
|
|
67
|
-
const maxDepth = 10;
|
|
68
|
-
// Handle cases where the actual error object is stringified inside the message
|
|
69
|
-
// by drilling down until we find an error that doesn't have a stringified message.
|
|
70
|
-
while (typeof currentError.message === 'string' && depth < maxDepth) {
|
|
71
|
-
try {
|
|
72
|
-
const parsedMessage = JSON.parse(currentError.message);
|
|
73
|
-
if (parsedMessage.error) {
|
|
74
|
-
currentError = parsedMessage.error;
|
|
75
|
-
depth++;
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
// The message is a JSON string, but not a nested error object.
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
catch (_) {
|
|
83
|
-
// It wasn't a JSON string, so we've drilled down as far as we can.
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
const code = responseStatus ?? currentError.code ?? gaxiosError.code;
|
|
88
|
-
const message = currentError.message;
|
|
89
|
-
const errorDetails = currentError.details;
|
|
90
|
-
if (Array.isArray(errorDetails) && code && message) {
|
|
91
|
-
const details = [];
|
|
92
|
-
for (const detail of errorDetails) {
|
|
93
|
-
if (detail && typeof detail === 'object' && '@type' in detail) {
|
|
94
|
-
// We can just cast it; the consumer will have to switch on @type
|
|
95
|
-
details.push(detail);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
if (details.length > 0) {
|
|
99
|
-
return {
|
|
100
|
-
code,
|
|
101
|
-
message,
|
|
102
|
-
details,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
//# sourceMappingURL=googleErrors.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"googleErrors.js","sourceRoot":"","sources":["../../../src/utils/googleErrors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA4GH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,QAAQ,GAAY,KAAK,CAAC;IAE9B,yCAAyC;IACzC,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,IAAI,CAAC;YACH,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,kCAAkC;YAClC,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtD,OAAO,IAAI,CAAC;IACd,CAAC;IAQD,MAAM,WAAW,GAAG,QAWnB,CAAC;IAEF,IAAI,UAAkC,CAAC;IACvC,IAAI,WAAW,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;QAC/B,IAAI,OAAO,WAAW,CAAC,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAClD,IA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D,MAAM;YACR,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,mEAAmE;YACnE,MAAM;QACR,CAAC;IACH,CAAC;IAED,MAAM,IAAI,GAAG,cAAc,IAAI,YAAY,CAAC,IAAI,IAAI,WAAW,CAAC,IAAI,CAAC;IACrE,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC;IACrC,MAAM,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC;IAE1C,IAAI,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,IAAI,IAAI,OAAO,EAAE,CAAC;QACnD,MAAM,OAAO,GAA2B,EAAE,CAAC;QAC3C,KAAK,MAAM,MAAM,IAAI,YAAY,EAAE,CAAC;YAClC,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,IAAI,MAAM,EAAE,CAAC;gBAC9D,iEAAiE;gBACjE,OAAO,CAAC,IAAI,CAAC,MAA8B,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QAED,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,OAAO;gBACL,IAAI;gBACJ,OAAO;gBACP,OAAO;aACR,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2025 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
import { describe, it, expect } from 'vitest';
|
|
7
|
-
import { parseGoogleApiError } from './googleErrors.js';
|
|
8
|
-
describe('parseGoogleApiError', () => {
|
|
9
|
-
it('should return null for non-gaxios errors', () => {
|
|
10
|
-
expect(parseGoogleApiError(new Error('vanilla error'))).toBeNull();
|
|
11
|
-
expect(parseGoogleApiError(null)).toBeNull();
|
|
12
|
-
expect(parseGoogleApiError({})).toBeNull();
|
|
13
|
-
});
|
|
14
|
-
it('should parse a standard gaxios error', () => {
|
|
15
|
-
const mockError = {
|
|
16
|
-
response: {
|
|
17
|
-
status: 429,
|
|
18
|
-
data: {
|
|
19
|
-
error: {
|
|
20
|
-
code: 429,
|
|
21
|
-
message: 'Quota exceeded',
|
|
22
|
-
details: [
|
|
23
|
-
{
|
|
24
|
-
'@type': 'type.googleapis.com/google.rpc.QuotaFailure',
|
|
25
|
-
violations: [{ subject: 'user', description: 'daily limit' }],
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
const parsed = parseGoogleApiError(mockError);
|
|
33
|
-
expect(parsed).not.toBeNull();
|
|
34
|
-
expect(parsed?.code).toBe(429);
|
|
35
|
-
expect(parsed?.message).toBe('Quota exceeded');
|
|
36
|
-
expect(parsed?.details).toHaveLength(1);
|
|
37
|
-
const detail = parsed?.details[0];
|
|
38
|
-
expect(detail['@type']).toBe('type.googleapis.com/google.rpc.QuotaFailure');
|
|
39
|
-
expect(detail.violations[0].description).toBe('daily limit');
|
|
40
|
-
});
|
|
41
|
-
it('should parse an error with details stringified in the message', () => {
|
|
42
|
-
const innerError = {
|
|
43
|
-
error: {
|
|
44
|
-
code: 429,
|
|
45
|
-
message: 'Inner quota message',
|
|
46
|
-
details: [
|
|
47
|
-
{
|
|
48
|
-
'@type': 'type.googleapis.com/google.rpc.RetryInfo',
|
|
49
|
-
retryDelay: '10s',
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
const mockError = {
|
|
55
|
-
response: {
|
|
56
|
-
status: 429,
|
|
57
|
-
data: {
|
|
58
|
-
error: {
|
|
59
|
-
code: 429,
|
|
60
|
-
message: JSON.stringify(innerError),
|
|
61
|
-
details: [], // Top-level details are empty
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
const parsed = parseGoogleApiError(mockError);
|
|
67
|
-
expect(parsed).not.toBeNull();
|
|
68
|
-
expect(parsed?.code).toBe(429);
|
|
69
|
-
expect(parsed?.message).toBe('Inner quota message');
|
|
70
|
-
expect(parsed?.details).toHaveLength(1);
|
|
71
|
-
expect(parsed?.details[0]['@type']).toBe('type.googleapis.com/google.rpc.RetryInfo');
|
|
72
|
-
});
|
|
73
|
-
it('should return null if details are not in the expected format', () => {
|
|
74
|
-
const mockError = {
|
|
75
|
-
response: {
|
|
76
|
-
status: 400,
|
|
77
|
-
data: {
|
|
78
|
-
error: {
|
|
79
|
-
code: 400,
|
|
80
|
-
message: 'Bad Request',
|
|
81
|
-
details: 'just a string', // Invalid details format
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
expect(parseGoogleApiError(mockError)).toBeNull();
|
|
87
|
-
});
|
|
88
|
-
it('should return null if there are no valid details', () => {
|
|
89
|
-
const mockError = {
|
|
90
|
-
response: {
|
|
91
|
-
status: 400,
|
|
92
|
-
data: {
|
|
93
|
-
error: {
|
|
94
|
-
code: 400,
|
|
95
|
-
message: 'Bad Request',
|
|
96
|
-
details: [
|
|
97
|
-
{
|
|
98
|
-
// missing '@type'
|
|
99
|
-
reason: 'some reason',
|
|
100
|
-
},
|
|
101
|
-
],
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
expect(parseGoogleApiError(mockError)).toBeNull();
|
|
107
|
-
});
|
|
108
|
-
it('should parse a doubly nested error in the message', () => {
|
|
109
|
-
const innerError = {
|
|
110
|
-
error: {
|
|
111
|
-
code: 429,
|
|
112
|
-
message: 'Innermost quota message',
|
|
113
|
-
details: [
|
|
114
|
-
{
|
|
115
|
-
'@type': 'type.googleapis.com/google.rpc.RetryInfo',
|
|
116
|
-
retryDelay: '20s',
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
const middleError = {
|
|
122
|
-
error: {
|
|
123
|
-
code: 429,
|
|
124
|
-
message: JSON.stringify(innerError),
|
|
125
|
-
details: [],
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
const mockError = {
|
|
129
|
-
response: {
|
|
130
|
-
status: 429,
|
|
131
|
-
data: {
|
|
132
|
-
error: {
|
|
133
|
-
code: 429,
|
|
134
|
-
message: JSON.stringify(middleError),
|
|
135
|
-
details: [],
|
|
136
|
-
},
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
};
|
|
140
|
-
const parsed = parseGoogleApiError(mockError);
|
|
141
|
-
expect(parsed).not.toBeNull();
|
|
142
|
-
expect(parsed?.code).toBe(429);
|
|
143
|
-
expect(parsed?.message).toBe('Innermost quota message');
|
|
144
|
-
expect(parsed?.details).toHaveLength(1);
|
|
145
|
-
expect(parsed?.details[0]['@type']).toBe('type.googleapis.com/google.rpc.RetryInfo');
|
|
146
|
-
});
|
|
147
|
-
it('should parse an error that is not in a response object', () => {
|
|
148
|
-
const innerError = {
|
|
149
|
-
error: {
|
|
150
|
-
code: 429,
|
|
151
|
-
message: 'Innermost quota message',
|
|
152
|
-
details: [
|
|
153
|
-
{
|
|
154
|
-
'@type': 'type.googleapis.com/google.rpc.RetryInfo',
|
|
155
|
-
retryDelay: '20s',
|
|
156
|
-
},
|
|
157
|
-
],
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
const mockError = {
|
|
161
|
-
error: {
|
|
162
|
-
code: 429,
|
|
163
|
-
message: JSON.stringify(innerError),
|
|
164
|
-
details: [],
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
const parsed = parseGoogleApiError(mockError);
|
|
168
|
-
expect(parsed).not.toBeNull();
|
|
169
|
-
expect(parsed?.code).toBe(429);
|
|
170
|
-
expect(parsed?.message).toBe('Innermost quota message');
|
|
171
|
-
expect(parsed?.details).toHaveLength(1);
|
|
172
|
-
expect(parsed?.details[0]['@type']).toBe('type.googleapis.com/google.rpc.RetryInfo');
|
|
173
|
-
});
|
|
174
|
-
it('should parse an error that is a JSON string', () => {
|
|
175
|
-
const innerError = {
|
|
176
|
-
error: {
|
|
177
|
-
code: 429,
|
|
178
|
-
message: 'Innermost quota message',
|
|
179
|
-
details: [
|
|
180
|
-
{
|
|
181
|
-
'@type': 'type.googleapis.com/google.rpc.RetryInfo',
|
|
182
|
-
retryDelay: '20s',
|
|
183
|
-
},
|
|
184
|
-
],
|
|
185
|
-
},
|
|
186
|
-
};
|
|
187
|
-
const mockError = {
|
|
188
|
-
error: {
|
|
189
|
-
code: 429,
|
|
190
|
-
message: JSON.stringify(innerError),
|
|
191
|
-
details: [],
|
|
192
|
-
},
|
|
193
|
-
};
|
|
194
|
-
const parsed = parseGoogleApiError(JSON.stringify(mockError));
|
|
195
|
-
expect(parsed).not.toBeNull();
|
|
196
|
-
expect(parsed?.code).toBe(429);
|
|
197
|
-
expect(parsed?.message).toBe('Innermost quota message');
|
|
198
|
-
expect(parsed?.details).toHaveLength(1);
|
|
199
|
-
expect(parsed?.details[0]['@type']).toBe('type.googleapis.com/google.rpc.RetryInfo');
|
|
200
|
-
});
|
|
201
|
-
it('should parse the user-provided nested error string', () => {
|
|
202
|
-
const userErrorString = '{"error":{"message":"{\\n \\"error\\": {\\n \\"code\\": 429,\\n \\"message\\": \\"You exceeded your current quota, please check your plan and billing details. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits.\\\\n* Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_paid_tier_input_token_count, limit: 10000\\\\nPlease retry in 40.025771073s.\\",\\n \\"status\\": \\"RESOURCE_EXHAUSTED\\",\\n \\"details\\": [\\n {\\n \\"@type\\": \\"type.googleapis.com/google.rpc.DebugInfo\\",\\n \\"detail\\": \\"[ORIGINAL ERROR] generic::resource_exhausted: You exceeded your current quota, please check your plan and billing details. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits.\\\\n* Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_paid_tier_input_token_count, limit: 10000\\\\nPlease retry in 40.025771073s. [google.rpc.error_details_ext] { message: \\\\\\"You exceeded your current quota, please check your plan and billing details. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits.\\\\\\\\n* Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_paid_tier_input_token_count, limit: 10000\\\\\\\\nPlease retry in 40.025771073s.\\\\\\" }\\"\\n },\\n {\\n \\"@type\\": \\"type.googleapis.com/google.rpc.QuotaFailure\\",\\n \\"violations\\": [\\n {\\n \\"quotaMetric\\": \\"generativelanguage.googleapis.com/generate_content_paid_tier_input_token_count\\",\\n \\"quotaId\\": \\"GenerateContentPaidTierInputTokensPerModelPerMinute\\",\\n \\"quotaDimensions\\": {\\n \\"location\\": \\"global\\",\\n \\"model\\": \\"gemini-2.5-pro\\"\\n },\\n \\"quotaValue\\": \\"10000\\"\\n }\\n ]\\n },\\n {\\n \\"@type\\": \\"type.googleapis.com/google.rpc.Help\\",\\n \\"links\\": [\\n {\\n \\"description\\": \\"Learn more about Gemini API quotas\\",\\n \\"url\\": \\"https://ai.google.dev/gemini-api/docs/rate-limits\\"\\n }\\n ]\\n },\\n {\\n \\"@type\\": \\"type.googleapis.com/google.rpc.RetryInfo\\",\\n \\"retryDelay\\": \\"40s\\"\\n }\\n ]\\n }\\n}\\n","code":429,"status":"Too Many Requests"}}';
|
|
203
|
-
const parsed = parseGoogleApiError(userErrorString);
|
|
204
|
-
expect(parsed).not.toBeNull();
|
|
205
|
-
expect(parsed?.code).toBe(429);
|
|
206
|
-
expect(parsed?.message).toContain('You exceeded your current quota');
|
|
207
|
-
expect(parsed?.details).toHaveLength(4);
|
|
208
|
-
expect(parsed?.details.some((d) => d['@type'] === 'type.googleapis.com/google.rpc.QuotaFailure')).toBe(true);
|
|
209
|
-
expect(parsed?.details.some((d) => d['@type'] === 'type.googleapis.com/google.rpc.RetryInfo')).toBe(true);
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
//# sourceMappingURL=google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DX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
import type { GoogleApiError } from './googleErrors.js';
|
|
7
|
-
/**
|
|
8
|
-
* A non-retryable error indicating a hard quota limit has been reached (e.g., daily limit).
|
|
9
|
-
*/
|
|
10
|
-
export declare class TerminalQuotaError extends Error {
|
|
11
|
-
readonly cause: GoogleApiError;
|
|
12
|
-
constructor(message: string, cause: GoogleApiError);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* A retryable error indicating a temporary quota issue (e.g., per-minute limit).
|
|
16
|
-
*/
|
|
17
|
-
export declare class RetryableQuotaError extends Error {
|
|
18
|
-
readonly cause: GoogleApiError;
|
|
19
|
-
retryDelayMs: number;
|
|
20
|
-
constructor(message: string, cause: GoogleApiError, retryDelaySeconds: number);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Analyzes a caught error and classifies it as a specific quota-related error if applicable.
|
|
24
|
-
*
|
|
25
|
-
* It decides whether an error is a `TerminalQuotaError` or a `RetryableQuotaError` based on
|
|
26
|
-
* the following logic:
|
|
27
|
-
* - If the error indicates a daily limit, it's a `TerminalQuotaError`.
|
|
28
|
-
* - If the error suggests a retry delay of more than 5 minutes, it's a `TerminalQuotaError`.
|
|
29
|
-
* - If the error suggests a retry delay of 5 minutes or less, it's a `RetryableQuotaError`.
|
|
30
|
-
* - If the error indicates a per-minute limit, it's a `RetryableQuotaError`.
|
|
31
|
-
*
|
|
32
|
-
* @param error The error to classify.
|
|
33
|
-
* @returns A `TerminalQuotaError`, `RetryableQuotaError`, or the original `unknown` error.
|
|
34
|
-
*/
|
|
35
|
-
export declare function classifyGoogleError(error: unknown): unknown;
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2025 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
import { parseGoogleApiError } from './googleErrors.js';
|
|
7
|
-
const FIVE_MINUTES_IN_SECONDS = 5 * 60;
|
|
8
|
-
/**
|
|
9
|
-
* A non-retryable error indicating a hard quota limit has been reached (e.g., daily limit).
|
|
10
|
-
*/
|
|
11
|
-
export class TerminalQuotaError extends Error {
|
|
12
|
-
cause;
|
|
13
|
-
constructor(message, cause) {
|
|
14
|
-
super(message);
|
|
15
|
-
this.cause = cause;
|
|
16
|
-
this.name = 'TerminalQuotaError';
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* A retryable error indicating a temporary quota issue (e.g., per-minute limit).
|
|
21
|
-
*/
|
|
22
|
-
export class RetryableQuotaError extends Error {
|
|
23
|
-
cause;
|
|
24
|
-
retryDelayMs;
|
|
25
|
-
constructor(message, cause, retryDelaySeconds) {
|
|
26
|
-
super(message);
|
|
27
|
-
this.cause = cause;
|
|
28
|
-
this.name = 'RetryableQuotaError';
|
|
29
|
-
this.retryDelayMs = retryDelaySeconds * 1000;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Parses a duration string (e.g., "34.074824224s", "60s") and returns the time in seconds.
|
|
34
|
-
* @param duration The duration string to parse.
|
|
35
|
-
* @returns The duration in seconds, or null if parsing fails.
|
|
36
|
-
*/
|
|
37
|
-
function parseDurationInSeconds(duration) {
|
|
38
|
-
if (!duration.endsWith('s')) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
const seconds = parseFloat(duration.slice(0, -1));
|
|
42
|
-
return isNaN(seconds) ? null : seconds;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Analyzes a caught error and classifies it as a specific quota-related error if applicable.
|
|
46
|
-
*
|
|
47
|
-
* It decides whether an error is a `TerminalQuotaError` or a `RetryableQuotaError` based on
|
|
48
|
-
* the following logic:
|
|
49
|
-
* - If the error indicates a daily limit, it's a `TerminalQuotaError`.
|
|
50
|
-
* - If the error suggests a retry delay of more than 5 minutes, it's a `TerminalQuotaError`.
|
|
51
|
-
* - If the error suggests a retry delay of 5 minutes or less, it's a `RetryableQuotaError`.
|
|
52
|
-
* - If the error indicates a per-minute limit, it's a `RetryableQuotaError`.
|
|
53
|
-
*
|
|
54
|
-
* @param error The error to classify.
|
|
55
|
-
* @returns A `TerminalQuotaError`, `RetryableQuotaError`, or the original `unknown` error.
|
|
56
|
-
*/
|
|
57
|
-
export function classifyGoogleError(error) {
|
|
58
|
-
const googleApiError = parseGoogleApiError(error);
|
|
59
|
-
if (!googleApiError || googleApiError.code !== 429) {
|
|
60
|
-
return error; // Not a 429 error we can handle.
|
|
61
|
-
}
|
|
62
|
-
const quotaFailure = googleApiError.details.find((d) => d['@type'] === 'type.googleapis.com/google.rpc.QuotaFailure');
|
|
63
|
-
const errorInfo = googleApiError.details.find((d) => d['@type'] === 'type.googleapis.com/google.rpc.ErrorInfo');
|
|
64
|
-
const retryInfo = googleApiError.details.find((d) => d['@type'] === 'type.googleapis.com/google.rpc.RetryInfo');
|
|
65
|
-
// 1. Check for long-term limits in QuotaFailure or ErrorInfo
|
|
66
|
-
if (quotaFailure) {
|
|
67
|
-
for (const violation of quotaFailure.violations) {
|
|
68
|
-
const quotaId = violation.quotaId ?? '';
|
|
69
|
-
if (quotaId.includes('PerDay') || quotaId.includes('Daily')) {
|
|
70
|
-
return new TerminalQuotaError(`Reached a daily quota limit: ${violation.description}`, googleApiError);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (errorInfo) {
|
|
75
|
-
const quotaLimit = errorInfo.metadata?.['quota_limit'] ?? '';
|
|
76
|
-
if (quotaLimit.includes('PerDay') || quotaLimit.includes('Daily')) {
|
|
77
|
-
return new TerminalQuotaError(`Reached a daily quota limit: ${errorInfo.reason}`, googleApiError);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
// 2. Check for long delays in RetryInfo
|
|
81
|
-
if (retryInfo?.retryDelay) {
|
|
82
|
-
const delaySeconds = parseDurationInSeconds(retryInfo.retryDelay);
|
|
83
|
-
if (delaySeconds !== null) {
|
|
84
|
-
if (delaySeconds > FIVE_MINUTES_IN_SECONDS) {
|
|
85
|
-
return new TerminalQuotaError(`Quota limit requires a long delay of ${retryInfo.retryDelay}.`, googleApiError);
|
|
86
|
-
}
|
|
87
|
-
// This is a retryable error with a specific delay.
|
|
88
|
-
return new RetryableQuotaError(`Quota limit hit. Retrying after ${retryInfo.retryDelay}.`, googleApiError, delaySeconds);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
// 3. Check for short-term limits in QuotaFailure or ErrorInfo
|
|
92
|
-
if (quotaFailure) {
|
|
93
|
-
for (const violation of quotaFailure.violations) {
|
|
94
|
-
const quotaId = violation.quotaId ?? '';
|
|
95
|
-
if (quotaId.includes('PerMinute')) {
|
|
96
|
-
return new RetryableQuotaError(`Quota limit hit: ${violation.description}. Retrying after 60s.`, googleApiError, 60);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
if (errorInfo) {
|
|
101
|
-
const quotaLimit = errorInfo.metadata?.['quota_limit'] ?? '';
|
|
102
|
-
if (quotaLimit.includes('PerMinute')) {
|
|
103
|
-
return new RetryableQuotaError(`Quota limit hit: ${errorInfo.reason}. Retrying after 60s.`, googleApiError, 60);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return error; // Fallback to original error if no specific classification fits.
|
|
107
|
-
}
|
|
108
|
-
//# sourceMappingURL=googleQuotaErrors.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"googleQuotaErrors.js","sourceRoot":"","sources":["../../../src/utils/googleQuotaErrors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,MAAM,uBAAuB,GAAG,CAAC,GAAG,EAAE,CAAC;AAEvC;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAGvB;IAFpB,YACE,OAAe,EACG,K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