From 6cd128c09330c8787d1f9cbf987f0b17b2d828c2 Mon Sep 17 00:00:00 2001 From: Deyaaeldeen Almahallawi Date: Wed, 15 Sep 2021 15:08:56 -0400 Subject: [PATCH] [Schema Registry] No longer return an undefined and throw instead (#17700) --- .../schema-registry/CHANGELOG.md | 1 + .../review/schema-registry.api.md | 7 +-- .../src/schemaRegistryClient.ts | 60 +++++++------------ .../test/schemaRegistry.spec.ts | 4 +- 4 files changed, 29 insertions(+), 43 deletions(-) diff --git a/sdk/schemaregistry/schema-registry/CHANGELOG.md b/sdk/schemaregistry/schema-registry/CHANGELOG.md index ba509faedf52..de32a70a478b 100644 --- a/sdk/schemaregistry/schema-registry/CHANGELOG.md +++ b/sdk/schemaregistry/schema-registry/CHANGELOG.md @@ -10,6 +10,7 @@ - renames `SchemaId` to `SchemaProperties` - renames `getSchemaById` to `getSchema` - renames `GetSchemaByIdOptions` to `GetSchemaOptions` +- `getSchema` and `getSchemaProperties` no longer return `undefined` if the schema was not registered ### Bugs Fixed diff --git a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md index 649b216f25c8..4db4fa8842bc 100644 --- a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md +++ b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md @@ -56,16 +56,15 @@ export interface SchemaRegistry { export class SchemaRegistryClient implements SchemaRegistry { constructor(endpoint: string, credential: TokenCredential, options?: SchemaRegistryClientOptions); readonly endpoint: string; - getSchema(id: string, options?: GetSchemaOptions): Promise; - getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise; + getSchema(id: string, options?: GetSchemaOptions): Promise; + getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise; registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise; - } +} // @public export interface SchemaRegistryClientOptions extends CommonClientOptions { } - // (No @packageDocumentation comment for this package) ``` diff --git a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts index 3f353de3bfb3..ee694df9b940 100644 --- a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts +++ b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts @@ -107,29 +107,22 @@ export class SchemaRegistryClient implements SchemaRegistry { async getSchemaProperties( schema: SchemaDescription, options?: GetSchemaPropertiesOptions - ): Promise { + ): Promise { const cached = this.schemaToIdMap.get(schema); if (cached !== undefined) { return cached; } - try { - const id = await this.client.schema - .queryIdByContent( - schema.groupName, - schema.name, - schema.serializationType, - schema.content, - options - ) - .then(convertSchemaIdResponse); - this.addToCache(schema, id); - return id; - } catch (error) { - if (typeof error === "object" && error?.statusCode === 404) { - return undefined; - } - throw error; - } + const id = await this.client.schema + .queryIdByContent( + schema.groupName, + schema.name, + schema.serializationType, + schema.content, + options + ) + .then(convertSchemaIdResponse); + this.addToCache(schema, id); + return id; } /** @@ -138,27 +131,20 @@ export class SchemaRegistryClient implements SchemaRegistry { * @param id - Unique schema ID. * @returns Schema with given ID or undefined if no schema was found with the given ID. */ - async getSchema(id: string, options?: GetSchemaOptions): Promise { + async getSchema(id: string, options?: GetSchemaOptions): Promise { const cached = this.idToSchemaMap.get(id); if (cached !== undefined) { return cached; } - try { - const { flatResponse, rawResponse } = await getRawResponse( - (paramOptions) => this.client.schema.getById(id, paramOptions), - options || {} - ); - const schema = convertSchemaResponse(flatResponse, rawResponse); - // the service should send schema's name and group in separate headers so - // we can implement the other direction of the bidirectional caching. - // see https://github.com/Azure/azure-sdk-for-js/issues/16763 - this.idToSchemaMap.set(id, schema); - return schema; - } catch (error) { - if (typeof error === "object" && error?.statusCode === 404) { - return undefined; - } - throw error; - } + const { flatResponse, rawResponse } = await getRawResponse( + (paramOptions) => this.client.schema.getById(id, paramOptions), + options || {} + ); + const schema = convertSchemaResponse(flatResponse, rawResponse); + // the service should send schema's name and group in separate headers so + // we can implement the other direction of the bidirectional caching. + // see https://github.com/Azure/azure-sdk-for-js/issues/16763 + this.idToSchemaMap.set(id, schema); + return schema; } } diff --git a/sdk/schemaregistry/schema-registry/test/schemaRegistry.spec.ts b/sdk/schemaregistry/schema-registry/test/schemaRegistry.spec.ts index db912c7ee83d..7d9f240a6a6b 100644 --- a/sdk/schemaregistry/schema-registry/test/schemaRegistry.spec.ts +++ b/sdk/schemaregistry/schema-registry/test/schemaRegistry.spec.ts @@ -110,7 +110,7 @@ describe("SchemaRegistryClient", function() { }); it("fails to get schema ID when no matching schema exists", async () => { - assert.isUndefined(await client.getSchemaProperties({ ...schema, name: "never-registered" })); + assert.isRejected(client.getSchemaProperties({ ...schema, name: "never-registered" })); }); it("gets schema ID", async () => { @@ -128,7 +128,7 @@ describe("SchemaRegistryClient", function() { }); it("fails to get schema when no schema exists with given ID", async () => { - assert.isUndefined(await client.getSchema("ffffffffffffffffffffffffffffffff")); + assert.isRejected(client.getSchema("ffffffffffffffffffffffffffffffff")); }); it("gets schema by ID", async () => {