@google/gemini-cli-core 0.11.0 → 0.11.2
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.11.1.tgz +0 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/src/config/config.js +1 -3
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/config.test.js +1 -1
- package/dist/src/config/config.test.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/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils/errorParsing.d.ts +1 -1
- package/dist/src/utils/errorParsing.js +5 -33
- package/dist/src/utils/errorParsing.js.map +1 -1
- package/dist/src/utils/errorParsing.test.js +0 -88
- package/dist/src/utils/errorParsing.test.js.map +1 -1
- package/dist/src/utils/flashFallback.test.js +26 -45
- package/dist/src/utils/flashFallback.test.js.map +1 -1
- package/dist/src/utils/googleErrors.d.ts +104 -0
- package/dist/src/utils/googleErrors.js +152 -0
- package/dist/src/utils/googleErrors.js.map +1 -0
- package/dist/src/utils/googleErrors.test.d.ts +6 -0
- package/dist/src/utils/googleErrors.test.js +301 -0
- package/dist/src/utils/googleErrors.test.js.map +1 -0
- package/dist/src/utils/googleQuotaErrors.d.ts +35 -0
- package/dist/src/utils/googleQuotaErrors.js +131 -0
- package/dist/src/utils/googleQuotaErrors.js.map +1 -0
- package/dist/src/utils/googleQuotaErrors.test.d.ts +6 -0
- package/dist/src/utils/googleQuotaErrors.test.js +281 -0
- package/dist/src/utils/googleQuotaErrors.test.js.map +1 -0
- package/dist/src/utils/quotaErrorDetection.d.ts +0 -2
- package/dist/src/utils/quotaErrorDetection.js +0 -46
- package/dist/src/utils/quotaErrorDetection.js.map +1 -1
- package/dist/src/utils/retry.js +41 -145
- package/dist/src/utils/retry.js.map +1 -1
- package/dist/src/utils/retry.test.js +31 -110
- package/dist/src/utils/retry.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorParsing.test.js","sourceRoot":"","sources":["../../../src/utils/errorParsing.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"errorParsing.test.js","sourceRoot":"","sources":["../../../src/utils/errorParsing.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAGvD,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;IACtC,MAAM,aAAa,GAAG,yCAAyC,CAAC;IAChE,MAAM,aAAa,GAAG,4CAA4C,CAAC;IAEnE,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;QAC9C,MAAM,YAAY,GAChB,6IAA6I,CAAC;QAChJ,MAAM,QAAQ,GACZ,yFAAyF,CAAC;QAC5F,MAAM,CAAC,sBAAsB,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wDAAwD,EAAE,GAAG,EAAE;QAChE,MAAM,YAAY,GAChB,yHAAyH,CAAC;QAC5H,MAAM,MAAM,GAAG,sBAAsB,CACnC,YAAY,EACZ,SAAS,EACT,S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AAE,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC7C,MAAM,KAAK,GAAG,KAAK,CAAC;QACpB,MAAM,QAAQ,GAAG,yCAAyC,CAAC;QAC3D,MAAM,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -6,14 +6,16 @@
|
|
|
6
6
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
7
7
|
import { Config } from '../config/config.js';
|
|
8
8
|
import fs from 'node:fs';
|
|
9
|
-
import { setSimulate429, disableSimulationAfterFallback, shouldSimulate429,
|
|
9
|
+
import { setSimulate429, disableSimulationAfterFallback, shouldSimulate429, resetRequestCounter, } from './testUtils.js';
|
|
10
10
|
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
|
|
11
11
|
import { retryWithBackoff } from './retry.js';
|
|
12
12
|
import { AuthType } from '../core/contentGenerator.js';
|
|
13
|
+
import { TerminalQuotaError } from './googleQuotaErrors.js';
|
|
13
14
|
vi.mock('node:fs');
|
|
14
15
|
// Update the description to reflect that this tests the retry utility's integration
|
|
15
16
|
describe('Retry Utility Fallback Integration', () => {
|
|
16
17
|
let config;
|
|
18
|
+
let mockGoogleApiError;
|
|
17
19
|
beforeEach(() => {
|
|
18
20
|
vi.mocked(fs.existsSync).mockReturnValue(true);
|
|
19
21
|
vi.mocked(fs.statSync).mockReturnValue({
|
|
@@ -26,6 +28,11 @@ describe('Retry Utility Fallback Integration', () => {
|
|
|
26
28
|
cwd: '/test',
|
|
27
29
|
model: 'gemini-2.5-pro',
|
|
28
30
|
});
|
|
31
|
+
mockGoogleApiError = {
|
|
32
|
+
code: 429,
|
|
33
|
+
message: 'mock error',
|
|
34
|
+
details: [],
|
|
35
|
+
};
|
|
29
36
|
// Reset simulation state for each test
|
|
30
37
|
setSimulate429(false);
|
|
31
38
|
resetRequestCounter();
|
|
@@ -37,75 +44,49 @@ describe('Retry Utility Fallback Integration', () => {
|
|
|
37
44
|
// Use the generalized setter
|
|
38
45
|
config.setFallbackModelHandler(fallbackHandler);
|
|
39
46
|
// Call the handler directly via the config property
|
|
40
|
-
const result = await config.fallbackModelHandler('gemini-2.5-pro', DEFAULT_GEMINI_FLASH_MODEL);
|
|
47
|
+
const result = await config.fallbackModelHandler('gemini-2.5-pro', DEFAULT_GEMINI_FLASH_MODEL, new Error('test'));
|
|
41
48
|
// Verify it returns the correct intent
|
|
42
49
|
expect(result).toBe('retry');
|
|
43
50
|
});
|
|
44
51
|
// This test validates the retry utility's logic for triggering the callback.
|
|
45
|
-
it('should trigger onPersistent429
|
|
52
|
+
it('should trigger onPersistent429 on TerminalQuotaError for OAuth users', async () => {
|
|
46
53
|
let fallbackCalled = false;
|
|
47
|
-
// Removed fallbackModel variable as it's no longer relevant here.
|
|
48
|
-
// Mock function that simulates exactly 2 429 errors, then succeeds after fallback
|
|
49
54
|
const mockApiCall = vi
|
|
50
55
|
.fn()
|
|
51
|
-
.mockRejectedValueOnce(
|
|
52
|
-
.mockRejectedValueOnce(
|
|
56
|
+
.mockRejectedValueOnce(new TerminalQuotaError('Daily limit', mockGoogleApiError))
|
|
57
|
+
.mockRejectedValueOnce(new TerminalQuotaError('Daily limit', mockGoogleApiError))
|
|
53
58
|
.mockResolvedValueOnce('success after fallback');
|
|
54
|
-
// Mock the onPersistent429 callback (this is what client.ts/geminiChat.ts provides)
|
|
55
59
|
const mockPersistent429Callback = vi.fn(async (_authType) => {
|
|
56
60
|
fallbackCalled = true;
|
|
57
|
-
// Return true to signal retryWithBackoff to reset attempts and continue.
|
|
58
61
|
return true;
|
|
59
62
|
});
|
|
60
|
-
// Test with OAuth personal auth type, with maxAttempts = 2 to ensure fallback triggers
|
|
61
63
|
const result = await retryWithBackoff(mockApiCall, {
|
|
62
64
|
maxAttempts: 2,
|
|
63
65
|
initialDelayMs: 1,
|
|
64
66
|
maxDelayMs: 10,
|
|
65
|
-
shouldRetryOnError: (error) => {
|
|
66
|
-
const status = error.status;
|
|
67
|
-
return status === 429;
|
|
68
|
-
},
|
|
69
67
|
onPersistent429: mockPersistent429Callback,
|
|
70
68
|
authType: AuthType.LOGIN_WITH_GOOGLE,
|
|
71
69
|
});
|
|
72
|
-
// Verify fallback mechanism was triggered
|
|
73
70
|
expect(fallbackCalled).toBe(true);
|
|
74
|
-
expect(mockPersistent429Callback).toHaveBeenCalledWith(AuthType.LOGIN_WITH_GOOGLE, expect.any(
|
|
71
|
+
expect(mockPersistent429Callback).toHaveBeenCalledWith(AuthType.LOGIN_WITH_GOOGLE, expect.any(TerminalQuotaError));
|
|
75
72
|
expect(result).toBe('success after fallback');
|
|
76
|
-
// Should have: 2 failures, then fallback triggered, then 1 success after retry reset
|
|
77
73
|
expect(mockApiCall).toHaveBeenCalledTimes(3);
|
|
78
74
|
});
|
|
79
75
|
it('should not trigger onPersistent429 for API key users', async () => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
76
|
+
const fallbackCallback = vi.fn();
|
|
77
|
+
const mockApiCall = vi
|
|
78
|
+
.fn()
|
|
79
|
+
.mockRejectedValueOnce(new TerminalQuotaError('Daily limit', mockGoogleApiError));
|
|
80
|
+
const promise = retryWithBackoff(mockApiCall, {
|
|
81
|
+
maxAttempts: 2,
|
|
82
|
+
initialDelayMs: 1,
|
|
83
|
+
maxDelayMs: 10,
|
|
84
|
+
onPersistent429: fallbackCallback,
|
|
85
|
+
authType: AuthType.USE_GEMINI, // API key auth type
|
|
87
86
|
});
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
maxAttempts: 5,
|
|
92
|
-
initialDelayMs: 10,
|
|
93
|
-
maxDelayMs: 100,
|
|
94
|
-
shouldRetryOnError: (error) => {
|
|
95
|
-
const status = error.status;
|
|
96
|
-
return status === 429;
|
|
97
|
-
},
|
|
98
|
-
onPersistent429: mockPersistent429Callback,
|
|
99
|
-
authType: AuthType.USE_GEMINI, // API key auth type
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
// Expected to throw after max attempts
|
|
104
|
-
expect(error.message).toContain('Rate limit exceeded');
|
|
105
|
-
}
|
|
106
|
-
// Verify fallback was NOT triggered for API key users
|
|
107
|
-
expect(fallbackCalled).toBe(false);
|
|
108
|
-
expect(mockPersistent429Callback).not.toHaveBeenCalled();
|
|
87
|
+
await expect(promise).rejects.toThrow('Daily limit');
|
|
88
|
+
expect(fallbackCallback).not.toHaveBeenCalled();
|
|
89
|
+
expect(mockApiCall).toHaveBeenCalledTimes(1);
|
|
109
90
|
});
|
|
110
91
|
// This test validates the test utilities themselves.
|
|
111
92
|
it('should properly disable simulation state after fallback (Test Utility)', () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flashFallback.test.js","sourceRoot":"","sources":["../../../src/utils/flashFallback.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EACL,cAAc,EACd,8BAA8B,EAC9B,iBAAiB,EACjB,
|
|
1
|
+
{"version":3,"file":"flashFallback.test.js","sourceRoot":"","sources":["../../../src/utils/flashFallback.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EACL,cAAc,EACd,8BAA8B,EAC9B,iBAA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@
|
|
|
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?: string | 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;
|
|
@@ -0,0 +1,152 @@
|
|
|
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 (Array.isArray(errorObj) && errorObj.length > 0) {
|
|
34
|
+
errorObj = errorObj[0];
|
|
35
|
+
}
|
|
36
|
+
if (typeof errorObj !== 'object' || errorObj === null) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
let currentError = fromGaxiosError(errorObj) ?? fromApiError(errorObj);
|
|
40
|
+
let depth = 0;
|
|
41
|
+
const maxDepth = 10;
|
|
42
|
+
// Handle cases where the actual error object is stringified inside the message
|
|
43
|
+
// by drilling down until we find an error that doesn't have a stringified message.
|
|
44
|
+
while (currentError &&
|
|
45
|
+
typeof currentError.message === 'string' &&
|
|
46
|
+
depth < maxDepth) {
|
|
47
|
+
try {
|
|
48
|
+
const parsedMessage = JSON.parse(currentError.message.replace(/\u00A0/g, '').replace(/\n/g, ' '));
|
|
49
|
+
if (parsedMessage.error) {
|
|
50
|
+
currentError = parsedMessage.error;
|
|
51
|
+
depth++;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// The message is a JSON string, but not a nested error object.
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (_error) {
|
|
59
|
+
// It wasn't a JSON string, so we've drilled down as far as we can.
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!currentError) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const code = currentError.code;
|
|
67
|
+
const message = currentError.message;
|
|
68
|
+
const errorDetails = currentError.details;
|
|
69
|
+
if (Array.isArray(errorDetails) && code && message) {
|
|
70
|
+
const details = [];
|
|
71
|
+
for (const detail of errorDetails) {
|
|
72
|
+
if (detail && typeof detail === 'object') {
|
|
73
|
+
const detailObj = detail;
|
|
74
|
+
const typeKey = Object.keys(detailObj).find((key) => key.trim() === '@type');
|
|
75
|
+
if (typeKey) {
|
|
76
|
+
if (typeKey !== '@type') {
|
|
77
|
+
detailObj['@type'] = detailObj[typeKey];
|
|
78
|
+
delete detailObj[typeKey];
|
|
79
|
+
}
|
|
80
|
+
// We can just cast it; the consumer will have to switch on @type
|
|
81
|
+
details.push(detailObj);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (details.length > 0) {
|
|
86
|
+
return {
|
|
87
|
+
code,
|
|
88
|
+
message,
|
|
89
|
+
details,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
function fromGaxiosError(errorObj) {
|
|
96
|
+
const gaxiosError = errorObj;
|
|
97
|
+
let outerError;
|
|
98
|
+
if (gaxiosError.response?.data) {
|
|
99
|
+
let data = gaxiosError.response.data;
|
|
100
|
+
if (typeof data === 'string') {
|
|
101
|
+
try {
|
|
102
|
+
data = JSON.parse(data);
|
|
103
|
+
}
|
|
104
|
+
catch (_) {
|
|
105
|
+
// Not a JSON string, can't parse.
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
109
|
+
data = data[0];
|
|
110
|
+
}
|
|
111
|
+
if (typeof data === 'object' && data !== null) {
|
|
112
|
+
if ('error' in data) {
|
|
113
|
+
outerError = data.error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!outerError) {
|
|
118
|
+
// If the gaxios structure isn't there, check for a top-level `error` property.
|
|
119
|
+
if (gaxiosError.error) {
|
|
120
|
+
outerError = gaxiosError.error;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return outerError;
|
|
127
|
+
}
|
|
128
|
+
function fromApiError(errorObj) {
|
|
129
|
+
const apiError = errorObj;
|
|
130
|
+
let outerError;
|
|
131
|
+
if (apiError.message) {
|
|
132
|
+
let data = apiError.message;
|
|
133
|
+
if (typeof data === 'string') {
|
|
134
|
+
try {
|
|
135
|
+
data = JSON.parse(data);
|
|
136
|
+
}
|
|
137
|
+
catch (_) {
|
|
138
|
+
// Not a JSON string, can't parse.
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
142
|
+
data = data[0];
|
|
143
|
+
}
|
|
144
|
+
if (typeof data === 'object' && data !== null) {
|
|
145
|
+
if ('error' in data) {
|
|
146
|
+
outerError = data.error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return outerError;
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=googleErrors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"googleErrors.js","sourceRoot":"","sources":["../../../src/utils/googleErrors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAkHH;;;;;;;;;;;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,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnD,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,YAAY,GACd,eAAe,CAAC,QAAQ,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,CAAC;IAEtD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,+EAA+E;IAC/E,mFAAmF;IACnF,OACE,YAAY;QACZ,OAAO,YAAY,CAAC,OAAO,KAAK,QAAQ;QACxC,KAAK,GAAG,QAAQ,EAChB,CAAC;QACD,IAAI,CAAC;YACH,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CA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