11
2- import { Client , Compatibility , SchemaInfo , SchemaMetadata , ServerConfig } from './schemaregistry-client' ;
2+ import {
3+ Client ,
4+ Compatibility ,
5+ minimize ,
6+ SchemaInfo ,
7+ SchemaMetadata ,
8+ ServerConfig
9+ } from './schemaregistry-client' ;
310import stringify from "json-stringify-deterministic" ;
411import { ClientConfig } from "./rest-service" ;
12+ import { RestError } from "./rest-error" ;
513
614interface VersionCacheEntry {
715 version : number ;
@@ -57,13 +65,13 @@ class MockClient implements Client {
5765 async register ( subject : string , schema : SchemaInfo , normalize : boolean = false ) : Promise < number > {
5866 const metadata = await this . registerFullResponse ( subject , schema , normalize ) ;
5967 if ( ! metadata ) {
60- throw new Error ( "Failed to register schema" ) ;
68+ throw new RestError ( "Failed to register schema" , 422 , 42200 ) ;
6169 }
6270 return metadata . id ;
6371 }
6472
6573 async registerFullResponse ( subject : string , schema : SchemaInfo , normalize : boolean = false ) : Promise < SchemaMetadata > {
66- const cacheKey = stringify ( { subject, schema } ) ;
74+ const cacheKey = stringify ( { subject, schema : minimize ( schema ) } ) ;
6775
6876 const cacheEntry = this . infoToSchemaCache . get ( cacheKey ) ;
6977 if ( cacheEntry && ! cacheEntry . softDeleted ) {
@@ -72,7 +80,7 @@ class MockClient implements Client {
7280
7381 const id = await this . getIDFromRegistry ( subject , schema ) ;
7482 if ( id === - 1 ) {
75- throw new Error ( "Failed to retrieve schema ID from registry" ) ;
83+ throw new RestError ( "Failed to retrieve schema ID from registry" , 422 , 42200 ) ;
7684 }
7785
7886 const metadata : SchemaMetadata = { ...schema , id } ;
@@ -112,7 +120,7 @@ class MockClient implements Client {
112120 newVersion = versions [ versions . length - 1 ] + 1 ;
113121 }
114122
115- const cacheKey = stringify ( { subject, schema : schema } ) ;
123+ const cacheKey = stringify ( { subject, schema : minimize ( schema ) } ) ;
116124 this . schemaToVersionCache . set ( cacheKey , { version : newVersion , softDeleted : false } ) ;
117125 }
118126
@@ -121,24 +129,24 @@ class MockClient implements Client {
121129 const cacheEntry = this . idToSchemaCache . get ( cacheKey ) ;
122130
123131 if ( ! cacheEntry || cacheEntry . softDeleted ) {
124- throw new Error ( "Schema not found" ) ;
132+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
125133 }
126134 return cacheEntry . info ;
127135 }
128136
129137 async getId ( subject : string , schema : SchemaInfo ) : Promise < number > {
130- const cacheKey = stringify ( { subject, schema } ) ;
138+ const cacheKey = stringify ( { subject, schema : minimize ( schema ) } ) ;
131139 const cacheEntry = this . infoToSchemaCache . get ( cacheKey ) ;
132140 if ( ! cacheEntry || cacheEntry . softDeleted ) {
133- throw new Error ( "Schema not found" ) ;
141+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
134142 }
135143 return cacheEntry . metadata . id ;
136144 }
137145
138146 async getLatestSchemaMetadata ( subject : string ) : Promise < SchemaMetadata > {
139147 const version = await this . latestVersion ( subject ) ;
140148 if ( version === - 1 ) {
141- throw new Error ( "No versions found for subject" ) ;
149+ throw new RestError ( "No versions found for subject" , 404 , 40400 ) ;
142150 }
143151
144152 return this . getSchemaMetadata ( subject , version ) ;
@@ -154,7 +162,7 @@ class MockClient implements Client {
154162 }
155163
156164 if ( ! json ) {
157- throw new Error ( "Schema not found" ) ;
165+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
158166 }
159167
160168 let id : number = - 1 ;
@@ -165,15 +173,15 @@ class MockClient implements Client {
165173 }
166174 }
167175 if ( id === - 1 ) {
168- throw new Error ( "Schema not found" ) ;
176+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
169177 }
170178
171179
172180 return {
173181 id,
174182 version,
175183 subject,
176- schema : json . schema . schema
184+ ... json . schema ,
177185 } ;
178186 }
179187
@@ -198,7 +206,7 @@ class MockClient implements Client {
198206 }
199207
200208 if ( results . length === 0 ) {
201- throw new Error ( "Schema not found" ) ;
209+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
202210 }
203211
204212 let latest : SchemaMetadata = results [ 0 ] ;
@@ -225,7 +233,7 @@ class MockClient implements Client {
225233 const results = await this . allVersions ( subject ) ;
226234
227235 if ( results . length === 0 ) {
228- throw new Error ( "No versions found for subject" ) ;
236+ throw new RestError ( "No versions found for subject" , 404 , 40400 ) ;
229237 }
230238 return results ;
231239 }
@@ -275,11 +283,11 @@ class MockClient implements Client {
275283 }
276284
277285 async getVersion ( subject : string , schema : SchemaInfo , normalize : boolean = false ) : Promise < number > {
278- const cacheKey = stringify ( { subject, schema } ) ;
286+ const cacheKey = stringify ( { subject, schema : minimize ( schema ) } ) ;
279287 const cacheEntry = this . schemaToVersionCache . get ( cacheKey ) ;
280288
281289 if ( ! cacheEntry || cacheEntry . softDeleted ) {
282- throw new Error ( "Schema not found" ) ;
290+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
283291 }
284292
285293 return cacheEntry . version ;
@@ -333,7 +341,7 @@ class MockClient implements Client {
333341 if ( parsedKey . subject === subject && value . version === version ) {
334342 await this . deleteVersion ( key , version , permanent ) ;
335343
336- const cacheKeySchema = stringify ( { subject, schema : parsedKey . schema } ) ;
344+ const cacheKeySchema = stringify ( { subject, schema : minimize ( parsedKey . schema ) } ) ;
337345 const cacheEntry = this . infoToSchemaCache . get ( cacheKeySchema ) ;
338346 if ( cacheEntry ) {
339347 await this . deleteMetadata ( cacheKeySchema , cacheEntry . metadata , permanent ) ;
@@ -363,7 +371,7 @@ class MockClient implements Client {
363371 async getCompatibility ( subject : string ) : Promise < Compatibility > {
364372 const cacheEntry = this . configCache . get ( subject ) ;
365373 if ( ! cacheEntry ) {
366- throw new Error ( "Subject not found" ) ;
374+ throw new RestError ( "Subject not found" , 404 , 40400 ) ;
367375 }
368376 return cacheEntry . compatibilityLevel as Compatibility ;
369377 }
@@ -376,7 +384,7 @@ class MockClient implements Client {
376384 async getDefaultCompatibility ( ) : Promise < Compatibility > {
377385 const cacheEntry = this . configCache . get ( noSubject ) ;
378386 if ( ! cacheEntry ) {
379- throw new Error ( "Default compatibility not found" ) ;
387+ throw new RestError ( "Default compatibility not found" , 404 , 40400 ) ;
380388 }
381389 return cacheEntry . compatibilityLevel as Compatibility ;
382390 }
@@ -389,7 +397,7 @@ class MockClient implements Client {
389397 async getConfig ( subject : string ) : Promise < ServerConfig > {
390398 const cacheEntry = this . configCache . get ( subject ) ;
391399 if ( ! cacheEntry ) {
392- throw new Error ( "Subject not found" ) ;
400+ throw new RestError ( "Subject not found" , 404 , 40400 ) ;
393401 }
394402 return cacheEntry ;
395403 }
@@ -402,7 +410,7 @@ class MockClient implements Client {
402410 async getDefaultConfig ( ) : Promise < ServerConfig > {
403411 const cacheEntry = this . configCache . get ( noSubject ) ;
404412 if ( ! cacheEntry ) {
405- throw new Error ( "Default config not found" ) ;
413+ throw new RestError ( "Default config not found" , 404 , 40400 ) ;
406414 }
407415 return cacheEntry ;
408416 }
0 commit comments