@@ -254,6 +254,15 @@ export class DataConnectApiClient {
254254 return tableName ;
255255 }
256256
257+ private handleBulkImportErrors ( err : FirebaseDataConnectError ) : never {
258+ if ( err . code === `data-connect/${ DATA_CONNECT_ERROR_CODE_MAPPING . QUERY_ERROR } ` ) {
259+ throw new FirebaseDataConnectError (
260+ DATA_CONNECT_ERROR_CODE_MAPPING . QUERY_ERROR ,
261+ `${ err . message } . Make sure that your table name passed in matches the type name in your GraphQL schema file.` ) ;
262+ }
263+ throw err ;
264+ }
265+
257266 /**
258267 * Insert a single row into the specified table.
259268 */
@@ -262,24 +271,31 @@ export class DataConnectApiClient {
262271 data : Variables ,
263272 ) : Promise < ExecuteGraphqlResponse < GraphQlResponse > > {
264273 if ( ! validator . isNonEmptyString ( tableName ) ) {
265- throw new FirebaseDataConnectError ( 'invalid-argument' , '`tableName` must be a non-empty string.' ) ;
274+ throw new FirebaseDataConnectError (
275+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
276+ '`tableName` must be a non-empty string.' ) ;
266277 }
267278 if ( validator . isArray ( data ) ) {
268279 throw new FirebaseDataConnectError (
269- 'invalid-argument' , '`data` must be an object, not an array, for single insert.' ) ;
280+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
281+ '`data` must be an object, not an array, for single insert.' ) ;
270282 }
271283 if ( ! validator . isNonNullObject ( data ) ) {
272- throw new FirebaseDataConnectError ( 'invalid-argument' , '`data` must be a non-null object.' ) ;
284+ throw new FirebaseDataConnectError (
285+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
286+ '`data` must be a non-null object.' ) ;
273287 }
274288
275- tableName = this . formatTableName ( tableName ) ;
276289 try {
290+ tableName = this . formatTableName ( tableName ) ;
277291 const gqlDataString = this . objectToString ( data ) ;
278292 const mutation = `mutation { ${ tableName } _insert(data: ${ gqlDataString } ) }` ;
279293 // Use internal executeGraphql
280- return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) ;
294+ return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) . catch ( this . handleBulkImportErrors ) ;
281295 } catch ( e : any ) {
282- throw new FirebaseDataConnectError ( 'internal-error' , `Failed to construct insert mutation: ${ e . message } ` ) ;
296+ throw new FirebaseDataConnectError (
297+ DATA_CONNECT_ERROR_CODE_MAPPING . INTERNAL ,
298+ `Failed to construct insert mutation: ${ e . message } ` ) ;
283299 }
284300 }
285301
@@ -291,20 +307,25 @@ export class DataConnectApiClient {
291307 data : Variables ,
292308 ) : Promise < ExecuteGraphqlResponse < GraphQlResponse > > {
293309 if ( ! validator . isNonEmptyString ( tableName ) ) {
294- throw new FirebaseDataConnectError ( 'invalid-argument' , '`tableName` must be a non-empty string.' ) ;
310+ throw new FirebaseDataConnectError (
311+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
312+ '`tableName` must be a non-empty string.' ) ;
295313 }
296314 if ( ! validator . isNonEmptyArray ( data ) ) {
297- throw new FirebaseDataConnectError ( 'invalid-argument' , '`data` must be a non-empty array for insertMany.' ) ;
315+ throw new FirebaseDataConnectError (
316+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
317+ '`data` must be a non-empty array for insertMany.' ) ;
298318 }
299319
300- tableName = this . formatTableName ( tableName ) ;
301320 try {
321+ tableName = this . formatTableName ( tableName ) ;
302322 const gqlDataString = this . objectToString ( data ) ;
303323 const mutation = `mutation { ${ tableName } _insertMany(data: ${ gqlDataString } ) }` ;
304324 // Use internal executeGraphql
305- return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) ;
325+ return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) . catch ( this . handleBulkImportErrors ) ;
306326 } catch ( e : any ) {
307- throw new FirebaseDataConnectError ( 'internal-error' , `Failed to construct insertMany mutation: ${ e . message } ` ) ;
327+ throw new FirebaseDataConnectError ( DATA_CONNECT_ERROR_CODE_MAPPING . INTERNAL ,
328+ `Failed to construct insertMany mutation: ${ e . message } ` ) ;
308329 }
309330 }
310331
@@ -316,24 +337,31 @@ export class DataConnectApiClient {
316337 data : Variables ,
317338 ) : Promise < ExecuteGraphqlResponse < GraphQlResponse > > {
318339 if ( ! validator . isNonEmptyString ( tableName ) ) {
319- throw new FirebaseDataConnectError ( 'invalid-argument' , '`tableName` must be a non-empty string.' ) ;
340+ throw new FirebaseDataConnectError (
341+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
342+ '`tableName` must be a non-empty string.' ) ;
320343 }
321344 if ( validator . isArray ( data ) ) {
322345 throw new FirebaseDataConnectError (
323- 'invalid-argument' , '`data` must be an object, not an array, for single upsert.' ) ;
346+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
347+ '`data` must be an object, not an array, for single upsert.' ) ;
324348 }
325349 if ( ! validator . isNonNullObject ( data ) ) {
326- throw new FirebaseDataConnectError ( 'invalid-argument' , '`data` must be a non-null object.' ) ;
350+ throw new FirebaseDataConnectError (
351+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
352+ '`data` must be a non-null object.' ) ;
327353 }
328354
329- tableName = this . formatTableName ( tableName ) ;
330355 try {
356+ tableName = this . formatTableName ( tableName ) ;
331357 const gqlDataString = this . objectToString ( data ) ;
332358 const mutation = `mutation { ${ tableName } _upsert(data: ${ gqlDataString } ) }` ;
333359 // Use internal executeGraphql
334- return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) ;
360+ return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) . catch ( this . handleBulkImportErrors ) ;
335361 } catch ( e : any ) {
336- throw new FirebaseDataConnectError ( 'internal-error' , `Failed to construct upsert mutation: ${ e . message } ` ) ;
362+ throw new FirebaseDataConnectError (
363+ DATA_CONNECT_ERROR_CODE_MAPPING . INTERNAL ,
364+ `Failed to construct upsert mutation: ${ e . message } ` ) ;
337365 }
338366 }
339367
@@ -345,20 +373,26 @@ export class DataConnectApiClient {
345373 data : Variables ,
346374 ) : Promise < ExecuteGraphqlResponse < GraphQlResponse > > {
347375 if ( ! validator . isNonEmptyString ( tableName ) ) {
348- throw new FirebaseDataConnectError ( 'invalid-argument' , '`tableName` must be a non-empty string.' ) ;
376+ throw new FirebaseDataConnectError (
377+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
378+ '`tableName` must be a non-empty string.' ) ;
349379 }
350380 if ( ! validator . isNonEmptyArray ( data ) ) {
351- throw new FirebaseDataConnectError ( 'invalid-argument' , '`data` must be a non-empty array for upsertMany.' ) ;
381+ throw new FirebaseDataConnectError (
382+ DATA_CONNECT_ERROR_CODE_MAPPING . INVALID_ARGUMENT ,
383+ '`data` must be a non-empty array for upsertMany.' ) ;
352384 }
353385
354- tableName = this . formatTableName ( tableName ) ;
355386 try {
387+ tableName = this . formatTableName ( tableName ) ;
356388 const gqlDataString = this . objectToString ( data ) ;
357389 const mutation = `mutation { ${ tableName } _upsertMany(data: ${ gqlDataString } ) }` ;
358390 // Use internal executeGraphql
359- return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) ;
391+ return this . executeGraphql < GraphQlResponse , Variables > ( mutation ) . catch ( this . handleBulkImportErrors ) ;
360392 } catch ( e : any ) {
361- throw new FirebaseDataConnectError ( 'internal-error' , `Failed to construct upsertMany mutation: ${ e . message } ` ) ;
393+ throw new FirebaseDataConnectError (
394+ DATA_CONNECT_ERROR_CODE_MAPPING . INTERNAL ,
395+ `Failed to construct upsertMany mutation: ${ e . message } ` ) ;
362396 }
363397 }
364398}
0 commit comments