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

[Schema Registry] Return undefined on 404 codes #18018

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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