Skip to content

Commit

Permalink
[Schema Registry] Return undefined on 404 codes (#18018)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyaaeldeen authored Oct 4, 2021
1 parent 520db84 commit c2a25c7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema>;
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties>;
getSchema(id: string, options?: GetSchemaOptions): Promise<Schema | undefined>;
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties | undefined>;
registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise<SchemaProperties>;
}

Expand Down
35 changes: 19 additions & 16 deletions sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -95,16 +95,18 @@ export class SchemaRegistryClient implements SchemaRegistry {
async getSchemaProperties(
schema: SchemaDescription,
options?: GetSchemaPropertiesOptions
): Promise<SchemaProperties> {
return this.client.schema
.queryIdByContent(
schema.groupName,
schema.name,
schema.format,
schema.schemaDefinition,
options
)
.then(convertSchemaIdResponse);
): Promise<SchemaProperties | undefined> {
return undefinedfyOn404(
this.client.schema
.queryIdByContent(
schema.groupName,
schema.name,
schema.format,
schema.schemaDefinition,
options
)
.then(convertSchemaIdResponse)
);
}

/**
Expand All @@ -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<Schema> {
const { flatResponse, rawResponse } = await getRawResponse(
(paramOptions) => this.client.schema.getById(id, paramOptions),
options || {}
async getSchema(id: string, options?: GetSchemaOptions): Promise<Schema | undefined> {
return undefinedfyOn404(
getRawResponse(
(paramOptions) => this.client.schema.getById(id, paramOptions),
options || {}
).then(({ flatResponse, rawResponse }) => convertSchemaResponse(flatResponse, rawResponse))
);
return convertSchemaResponse(flatResponse, rawResponse);
}
}
13 changes: 13 additions & 0 deletions sdk/schemaregistry/schema-registry/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ export async function getRawResponse<TOptions extends OperationOptions, TResult>
});
return { flatResponse, rawResponse: rawResponse! };
}

// return undefined if the response has 404 code
export async function undefinedfyOn404<T>(c: Promise<T>): Promise<T | undefined> {
try {
const r = await c;
return r;
} catch (e) {
if (typeof e === "object" && (e as { statusCode: number })?.statusCode === 404) {
return undefined;
}
throw e;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit c2a25c7

Please sign in to comment.