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] No longer return an undefined and throw instead #17700

Merged
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
1 change: 1 addition & 0 deletions sdk/schemaregistry/schema-registry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Schema | undefined>;
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties | undefined>;
getSchema(id: string, options?: GetSchemaOptions): Promise<Schema>;
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties>;
registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise<SchemaProperties>;
}
}

// @public
export interface SchemaRegistryClientOptions extends CommonClientOptions {
}


// (No @packageDocumentation comment for this package)

```
60 changes: 23 additions & 37 deletions sdk/schemaregistry/schema-registry/src/schemaRegistryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,22 @@ export class SchemaRegistryClient implements SchemaRegistry {
async getSchemaProperties(
schema: SchemaDescription,
options?: GetSchemaPropertiesOptions
): Promise<SchemaProperties | undefined> {
): Promise<SchemaProperties> {
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;
}

/**
Expand All @@ -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<Schema | undefined> {
async getSchema(id: string, options?: GetSchemaOptions): Promise<Schema> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down