diff --git a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md index 1e79bcebf0ca..92475fb15286 100644 --- a/sdk/schemaregistry/schema-registry/review/schema-registry.api.md +++ b/sdk/schemaregistry/schema-registry/review/schema-registry.api.md @@ -56,8 +56,8 @@ export interface SchemaRegistry { export class SchemaRegistryClient implements SchemaRegistry { constructor(fullyQualifiedNamespace: string, credential: TokenCredential, options?: SchemaRegistryClientOptions); readonly fullyQualifiedNamespace: 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; } diff --git a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts index 8093a11dee8d..439f0cf43405 100644 --- a/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts +++ b/sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts @@ -21,7 +21,7 @@ import { } from "./models"; import { DEFAULT_SCOPE } from "./constants"; import { logger } from "./logger"; -import { getRawResponse } from "./utils"; +import { getRawResponse, undefinedfyOn404 } from "./utils"; /** * Client for Azure Schema Registry service. @@ -95,16 +95,18 @@ export class SchemaRegistryClient implements SchemaRegistry { async getSchemaProperties( schema: SchemaDescription, options?: GetSchemaPropertiesOptions - ): Promise { - return this.client.schema - .queryIdByContent( - schema.groupName, - schema.name, - schema.format, - schema.schemaDefinition, - options - ) - .then(convertSchemaIdResponse); + ): Promise { + return undefinedfyOn404( + this.client.schema + .queryIdByContent( + schema.groupName, + schema.name, + schema.format, + schema.schemaDefinition, + options + ) + .then(convertSchemaIdResponse) + ); } /** @@ -113,11 +115,12 @@ 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 { - const { flatResponse, rawResponse } = await getRawResponse( - (paramOptions) => this.client.schema.getById(id, paramOptions), - options || {} + async getSchema(id: string, options?: GetSchemaOptions): Promise { + return undefinedfyOn404( + getRawResponse( + (paramOptions) => this.client.schema.getById(id, paramOptions), + options || {} + ).then(({ flatResponse, rawResponse }) => convertSchemaResponse(flatResponse, rawResponse)) ); - return convertSchemaResponse(flatResponse, rawResponse); } } diff --git a/sdk/schemaregistry/schema-registry/src/utils.ts b/sdk/schemaregistry/schema-registry/src/utils.ts index fd23656ccc7f..c99225cccf4e 100644 --- a/sdk/schemaregistry/schema-registry/src/utils.ts +++ b/sdk/schemaregistry/schema-registry/src/utils.ts @@ -23,3 +23,16 @@ export async function getRawResponse }); return { flatResponse, rawResponse: rawResponse! }; } + +// return undefined if the response has 404 code +export async function undefinedfyOn404(c: Promise): Promise { + try { + const r = await c; + return r; + } catch (e) { + if (typeof e === "object" && (e as { statusCode: number })?.statusCode === 404) { + return undefined; + } + throw e; + } +} diff --git a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts index c35165ac44fd..5239939ce05f 100644 --- a/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts +++ b/sdk/schemaregistry/schema-registry/test/public/schemaRegistry.spec.ts @@ -105,7 +105,7 @@ describe("SchemaRegistryClient", function() { }); it("fails to get schema ID when no matching schema exists", async () => { - await assert.isRejected(client.getSchemaProperties({ ...schema, name: "never-registered" })); + assert.isUndefined(await client.getSchemaProperties({ ...schema, name: "never-registered" })); }); it("gets schema ID", async () => { @@ -123,7 +123,7 @@ describe("SchemaRegistryClient", function() { }); it("fails to get schema when no schema exists with given ID", async () => { - await assert.isRejected(client.getSchema("ffffffffffffffffffffffffffffffff")); + assert.isUndefined(await client.getSchema("ffffffffffffffffffffffffffffffff")); }); it("gets schema by ID", async () => {