Skip to content

Commit

Permalink
fix error class
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Sep 11, 2024
1 parent e4beaf5 commit b57febe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
13 changes: 9 additions & 4 deletions packages/vertexai/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ describe('Top level API', () => {
getGenerativeModel(fakeVertexAI, {} as ModelParams);
} catch (e) {
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_MODEL);
expect((e as VertexAIError).message).equals(
`Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`
expect((e as VertexAIError).message).includes(
`VertexAI: Must provide a model name. Example: ` +
`getGenerativeModel({ model: 'my-model-name' }) (vertexAI/${VertexAIErrorCode.NO_MODEL})`
);
}
});
Expand All @@ -54,7 +55,9 @@ describe('Top level API', () => {
} catch (e) {
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_API_KEY);
expect((e as VertexAIError).message).equals(
`The "apiKey" field is empty in the local Firebase config. Firebase VertexAI requires this field to contain a valid API key.`
`VertexAI: The "apiKey" field is empty in the local ` +
`Firebase config. Firebase VertexAI requires this field to` +
` contain a valid API key. (vertexAI/${VertexAIErrorCode.NO_API_KEY})`
);
}
});
Expand All @@ -70,7 +73,9 @@ describe('Top level API', () => {
VertexAIErrorCode.NO_PROJECT_ID
);
expect((e as VertexAIError).message).equals(
`The "projectId" field is empty in the local Firebase config. Firebase VertexAI requires this field to contain a valid project ID.`
`VertexAI: The "projectId" field is empty in the local` +
` Firebase config. Firebase VertexAI requires this field ` +
`to contain a valid project ID. (vertexAI/${VertexAIErrorCode.NO_PROJECT_ID})`
);
}
});
Expand Down
6 changes: 3 additions & 3 deletions packages/vertexai/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ export class VertexAIError extends FirebaseError {
* @param customErrorData - Optional error data.
*/
constructor(
code: VertexAIErrorCode,
readonly code: VertexAIErrorCode,
message: string,
readonly customErrorData?: CustomErrorData
) {
// Match error format used by FirebaseError from ErrorFactory
const service = VERTEX_TYPE;
const serviceName = 'VertexAI';
const fullCode = `${service}/${code}`;
const fullMessage = `${serviceName}: ${message} (${fullCode}).`;
super(fullCode, fullMessage);
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
super(code, 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
Expand Down
2 changes: 1 addition & 1 deletion packages/vertexai/src/requests/schema-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { VertexAIErrorCode } from '../types';

use(sinonChai);

describe.only('Schema builder', () => {
describe('Schema builder', () => {
it('builds integer schema', () => {
const schema = Schema.integer();
expect(schema.toJSON()).to.eql({
Expand Down
27 changes: 9 additions & 18 deletions packages/vertexai/src/requests/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SchemaInterface,
SchemaType,
SchemaParams,
_SchemaRequest,
SchemaRequest,
ObjectSchemaInterface
} from '../types/schema';

Expand Down Expand Up @@ -56,7 +56,7 @@ export abstract class Schema implements SchemaInterface {
* Defines how this Schema should be serialized as JSON.
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior
*/
toJSON(): _SchemaRequest {
toJSON(): SchemaRequest {
const obj: { type: SchemaType; [key: string]: unknown } = {
type: this.type
};
Expand All @@ -67,7 +67,7 @@ export abstract class Schema implements SchemaInterface {
}
}
}
return obj as _SchemaRequest;
return obj as SchemaRequest;
}

static array(arrayParams: SchemaParams & { items: Schema }): ArraySchema {
Expand Down Expand Up @@ -181,15 +181,12 @@ export class StringSchema extends Schema {
this.enum = enumValues;
}

/**
* @internal
*/
toJSON(): _SchemaRequest {
toJSON(): SchemaRequest {
const obj = super.toJSON();
if (this.enum) {
obj['enum'] = this.enum;
}
return obj as _SchemaRequest;
return obj as SchemaRequest;
}
}

Expand All @@ -207,10 +204,7 @@ export class ArraySchema extends Schema {
});
}

/**
* @internal
*/
toJSON(): _SchemaRequest {
toJSON(): SchemaRequest {
const obj = super.toJSON();
obj.items = this.items.toJSON();
return obj;
Expand All @@ -236,10 +230,7 @@ export class ObjectSchema extends Schema {
});
}

/**
* @internal
*/
toJSON(): _SchemaRequest {
toJSON(): SchemaRequest {
const obj = super.toJSON();
obj.properties = { ...this.properties };
const required = [];
Expand All @@ -257,7 +248,7 @@ export class ObjectSchema extends Schema {
if (this.properties.hasOwnProperty(propertyKey)) {
obj.properties[propertyKey] = this.properties[
propertyKey
].toJSON() as _SchemaRequest;
].toJSON() as SchemaRequest;
if (!this.optionalProperties.includes(propertyKey)) {
required.push(propertyKey);
}
Expand All @@ -267,6 +258,6 @@ export class ObjectSchema extends Schema {
obj.required = required;
}
delete (obj as ObjectSchemaInterface).optionalProperties;
return obj as _SchemaRequest;
return obj as SchemaRequest;
}
}
5 changes: 2 additions & 3 deletions packages/vertexai/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ export interface SchemaParams extends SchemaShared<SchemaInterface> {}

/**
* Final format for Schema params passed to backend requests.
* @internal
* @public
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export interface _SchemaRequest extends SchemaShared<_SchemaRequest> {
export interface SchemaRequest extends SchemaShared<SchemaRequest> {
/**
* The type of the property. {@link
* SchemaType}.
Expand Down

0 comments on commit b57febe

Please sign in to comment.