|
14 | 14 | * See the License for the specific language governing permissions and |
15 | 15 | * limitations under the License. |
16 | 16 | */ |
| 17 | +import { FirebaseError } from '@firebase/util'; |
17 | 18 |
|
18 | | -import { ErrorFactory, ErrorMap } from '@firebase/util'; |
19 | | -import { GenerateContentResponse } from './types'; |
| 19 | +/** |
| 20 | + * Standardized error codes that {@link VertexAIError} can have. |
| 21 | + * |
| 22 | + * @public |
| 23 | + */ |
| 24 | +export const enum VertexAIErrorCode { |
| 25 | + /** A generic error occured. */ |
| 26 | + ERROR = 'error', |
20 | 27 |
|
21 | | -export const enum VertexError { |
| 28 | + /** An error occurred in a request */ |
| 29 | + REQUEST_ERROR = 'request-error', |
| 30 | + |
| 31 | + /** An error occured in a response. */ |
| 32 | + RESPONSE_ERROR = 'response-error', |
| 33 | + |
| 34 | + /** An error occurred while performing a fetch */ |
22 | 35 | FETCH_ERROR = 'fetch-error', |
| 36 | + |
| 37 | + /** An error associated with a Content object. */ |
23 | 38 | INVALID_CONTENT = 'invalid-content', |
| 39 | + |
| 40 | + /** An error occured due to a missing api key */ |
24 | 41 | NO_API_KEY = 'no-api-key', |
| 42 | + |
| 43 | + /** An error occurred due to a missing model */ |
25 | 44 | NO_MODEL = 'no-model', |
| 45 | + |
| 46 | + /** An error occured due to a missing project id */ |
26 | 47 | NO_PROJECT_ID = 'no-project-id', |
27 | | - PARSE_FAILED = 'parse-failed', |
28 | | - RESPONSE_ERROR = 'response-error' |
| 48 | + |
| 49 | + /** An error occured while parsing */ |
| 50 | + PARSE_FAILED = 'parse-failed' |
29 | 51 | } |
30 | 52 |
|
31 | | -const ERRORS: ErrorMap<VertexError> = { |
32 | | - [VertexError.FETCH_ERROR]: `Error fetching from {$url}: {$message}`, |
33 | | - [VertexError.INVALID_CONTENT]: `Content formatting error: {$message}`, |
34 | | - [VertexError.NO_API_KEY]: |
35 | | - `The "apiKey" field is empty in the local Firebase config. Firebase VertexAI requires this field to` + |
36 | | - `contain a valid API key.`, |
37 | | - [VertexError.NO_PROJECT_ID]: |
38 | | - `The "projectId" field is empty in the local Firebase config. Firebase VertexAI requires this field to` + |
39 | | - `contain a valid project ID.`, |
40 | | - [VertexError.NO_MODEL]: |
41 | | - `Must provide a model name. ` + |
42 | | - `Example: getGenerativeModel({ model: 'my-model-name' })`, |
43 | | - [VertexError.PARSE_FAILED]: `Parsing failed: {$message}`, |
44 | | - [VertexError.RESPONSE_ERROR]: |
45 | | - `Response error: {$message}. Response body stored in ` + |
46 | | - `error.customData.response` |
47 | | -}; |
48 | | - |
49 | | -interface ErrorParams { |
50 | | - [VertexError.FETCH_ERROR]: { url: string; message: string }; |
51 | | - [VertexError.INVALID_CONTENT]: { message: string }; |
52 | | - [VertexError.PARSE_FAILED]: { message: string }; |
53 | | - [VertexError.RESPONSE_ERROR]: { |
54 | | - message: string; |
55 | | - response: GenerateContentResponse; |
56 | | - }; |
| 53 | +/** |
| 54 | + * Details object that may be included in an error response. |
| 55 | + * |
| 56 | + * @public |
| 57 | + */ |
| 58 | +interface ErrorDetails { |
| 59 | + '@type'?: string; |
| 60 | + |
| 61 | + /** The reason for the error */ |
| 62 | + reason?: string; |
| 63 | + |
| 64 | + /** The domain where the error occured. */ |
| 65 | + domain?: string; |
| 66 | + |
| 67 | + /** Additonal metadata about the error. */ |
| 68 | + metadata?: Record<string, unknown>; |
| 69 | + |
| 70 | + /** Any other relevant information about the error. */ |
| 71 | + [key: string]: unknown; |
57 | 72 | } |
58 | 73 |
|
59 | | -export const ERROR_FACTORY = new ErrorFactory<VertexError, ErrorParams>( |
60 | | - 'vertexAI', |
61 | | - 'VertexAI', |
62 | | - ERRORS |
63 | | -); |
| 74 | +/** |
| 75 | + * Error class for the Firebase VertexAI SDK. |
| 76 | + * |
| 77 | + * @public |
| 78 | + */ |
| 79 | +export class VertexAIError extends FirebaseError { |
| 80 | + /** |
| 81 | + * Stack trace of the error. |
| 82 | + */ |
| 83 | + readonly stack?: string; |
| 84 | + |
| 85 | + /** |
| 86 | + * Creates a new VertexAIError instance. |
| 87 | + * |
| 88 | + * @param code - The error code from {@link VertexAIErrorCode}. |
| 89 | + * @param message - A human-readable message describing the error. |
| 90 | + * @param status - Optional HTTP status code of the error response. |
| 91 | + * @param statusText - Optional HTTP status text of the error response. |
| 92 | + * @param errorDetails - Optional additional details about the error. |
| 93 | + */ |
| 94 | + constructor( |
| 95 | + readonly code: VertexAIErrorCode, |
| 96 | + readonly message: string, |
| 97 | + readonly status?: number, |
| 98 | + readonly statusText?: string, |
| 99 | + readonly errorDetails?: ErrorDetails[] |
| 100 | + ) { |
| 101 | + // Match error format used by FirebaseError from ErrorFactory |
| 102 | + const service = 'vertex-ai'; |
| 103 | + const serviceName = 'VertexAI'; |
| 104 | + const fullCode = `${service}/${code}`; |
| 105 | + const fullMessage = `${serviceName}: ${message} (${fullCode})`; |
| 106 | + super(fullCode, fullMessage); |
| 107 | + } |
| 108 | +} |
0 commit comments