Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new VertexAI error type #8240

Merged
merged 19 commits into from
Jun 25, 2024
Prev Previous commit
Next Next commit
Move VertexAIError implementation away from types
  • Loading branch information
dlarocque committed Jun 24, 2024
commit 30cbf280051fad294a90cfadb020ea85ff077225
55 changes: 55 additions & 0 deletions packages/vertexai/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { FirebaseError } from "@firebase/util";
import { VertexAIErrorCode, ErrorDetails } from "./types";

/**
* Error class for the Firebase VertexAI SDK.
*
* @public
*/
export class VertexAIError extends FirebaseError {
/**
* Stack trace of the error.
*/
readonly stack?: string;

/**
* Creates a new VertexAIError instance.
*
* @param code - The error code from {@link VertexAIErrorCode}.
* @param message - A human-readable message describing the error.
* @param status - Optional HTTP status code of the error response.
* @param statusText - Optional HTTP status text of the error response.
* @param errorDetails - Optional additional details about the error.
*/
constructor(
readonly code: VertexAIErrorCode,
readonly message: string,
readonly status?: number,
readonly statusText?: string,
readonly errorDetails?: ErrorDetails[]
) {
// Match error format used by FirebaseError from ErrorFactory
const service = 'vertex-ai';
const serviceName = 'VertexAI';
const fullCode = `${service}/${code}`;
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
super(fullCode, fullMessage);

// FirebaseError initializes a stack trace, but it assumes the error is created from the error
// factory. Since we break this assumption, we set the stack trace to be originating from this
// constructor.
// This is only supported in V8.
if (Error.captureStackTrace) {
// Allows us to initialize the stack trace without including the constructor itself at the
// top level of the stack trace.
Error.captureStackTrace(this, VertexAIError);
}

// Allows instanceof VertexAIError in ES5/ES6
// https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, VertexAIError.prototype);

// Since Error is an interface, we don't inherit toString and so we define it ourselves.
this.toString = () => fullMessage;
}
}
56 changes: 1 addition & 55 deletions packages/vertexai/src/types/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { FirebaseError } from "@firebase/util";

/**
* Details object that may be included in an error response.
*
Expand Down Expand Up @@ -55,56 +53,4 @@ export const enum VertexAIErrorCode {
PARSE_FAILED = 'parse-failed'
}


/**
* Error class for the Firebase VertexAI SDK.
*
* @public
*/
export class VertexAIError extends FirebaseError {
/**
* Stack trace of the error.
*/
readonly stack?: string;

/**
* Creates a new VertexAIError instance.
*
* @param code - The error code from {@link VertexAIErrorCode}.
* @param message - A human-readable message describing the error.
* @param status - Optional HTTP status code of the error response.
* @param statusText - Optional HTTP status text of the error response.
* @param errorDetails - Optional additional details about the error.
*/
constructor(
readonly code: VertexAIErrorCode,
readonly message: string,
readonly status?: number,
readonly statusText?: string,
readonly errorDetails?: ErrorDetails[]
) {
// Match error format used by FirebaseError from ErrorFactory
const service = 'vertex-ai';
const serviceName = 'VertexAI';
const fullCode = `${service}/${code}`;
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
super(fullCode, fullMessage);

// FirebaseError initializes a stack trace, but it assumes the error is created from the error
// factory. Since we break this assumption, we set the stack trace to be originating from this
// constructor.
// This is only supported in V8.
if (Error.captureStackTrace) {
// Allows us to initialize the stack trace without including the constructor itself at the
// top level of the stack trace.
Error.captureStackTrace(this, VertexAIError);
}

// Allows instanceof VertexAIError in ES5/ES6
// https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, VertexAIError.prototype);

// Since Error is an interface, we don't inherit toString and so we define it ourselves.
this.toString = () => fullMessage;
}
}
export { VertexAIError } from '../errors';