@@ -23,6 +23,7 @@ import Parse from 'parse/node';
23
23
// @flow -disable-next
24
24
import _ from 'lodash' ;
25
25
import defaults from '../../../defaults' ;
26
+ import logger from '../../../logger' ;
26
27
27
28
// @flow -disable-next
28
29
const mongodb = require ( 'mongodb' ) ;
@@ -160,6 +161,16 @@ export class MongoStorageAdapter implements StorageAdapter {
160
161
return this . connectionPromise ;
161
162
}
162
163
164
+ handleError < T > ( error : ?Error ) : Promise < T > {
165
+ if ( error && error . code === 13 ) { // Unauthorized error
166
+ delete this . client ;
167
+ delete this . database ;
168
+ delete this . connectionPromise ;
169
+ logger . error ( 'Received unauthorized error' , { error : error } ) ;
170
+ }
171
+ throw error ;
172
+ }
173
+
163
174
handleShutdown ( ) {
164
175
if ( ! this . client ) {
165
176
return ;
@@ -170,7 +181,8 @@ export class MongoStorageAdapter implements StorageAdapter {
170
181
_adaptiveCollection ( name : string ) {
171
182
return this . connect ( )
172
183
. then ( ( ) => this . database . collection ( this . _collectionPrefix + name ) )
173
- . then ( rawCollection => new MongoCollection ( rawCollection ) ) ;
184
+ . then ( rawCollection => new MongoCollection ( rawCollection ) )
185
+ . catch ( err => this . handleError ( err ) ) ;
174
186
}
175
187
176
188
_schemaCollection ( ) : Promise < MongoSchemaCollection > {
@@ -184,14 +196,14 @@ export class MongoStorageAdapter implements StorageAdapter {
184
196
return this . database . listCollections ( { name : this . _collectionPrefix + name } ) . toArray ( ) ;
185
197
} ) . then ( collections => {
186
198
return collections . length > 0 ;
187
- } ) ;
199
+ } ) . catch ( err => this . handleError ( err ) ) ;
188
200
}
189
201
190
202
setClassLevelPermissions ( className : string , CLPs : any ) : Promise < void > {
191
203
return this . _schemaCollection ( )
192
204
. then ( schemaCollection => schemaCollection . updateSchema ( className , {
193
205
$set : { '_metadata.class_permissions' : CLPs }
194
- } ) ) ;
206
+ } ) ) . catch ( err => this . handleError ( err ) ) ;
195
207
}
196
208
197
209
setIndexesWithSchemaFormat ( className : string , submittedIndexes : any , existingIndexes : any = { } , fields : any ) : Promise < void > {
@@ -237,7 +249,8 @@ export class MongoStorageAdapter implements StorageAdapter {
237
249
. then ( ( ) => this . _schemaCollection ( ) )
238
250
. then ( schemaCollection => schemaCollection . updateSchema ( className , {
239
251
$set : { '_metadata.indexes' : existingIndexes }
240
- } ) ) ;
252
+ } ) )
253
+ . catch ( err => this . handleError ( err ) ) ;
241
254
}
242
255
243
256
setIndexesFromMongo ( className : string ) {
@@ -257,10 +270,12 @@ export class MongoStorageAdapter implements StorageAdapter {
257
270
. then ( schemaCollection => schemaCollection . updateSchema ( className , {
258
271
$set : { '_metadata.indexes' : indexes }
259
272
} ) ) ;
260
- } ) . catch ( ( ) => {
261
- // Ignore if collection not found
262
- return Promise . resolve ( ) ;
263
- } ) ;
273
+ } )
274
+ . catch ( err => this . handleError ( err ) )
275
+ . catch ( ( ) => {
276
+ // Ignore if collection not found
277
+ return Promise . resolve ( ) ;
278
+ } ) ;
264
279
}
265
280
266
281
createClass ( className : string , schema : SchemaType ) : Promise < void > {
@@ -269,13 +284,15 @@ export class MongoStorageAdapter implements StorageAdapter {
269
284
mongoObject . _id = className ;
270
285
return this . setIndexesWithSchemaFormat ( className , schema . indexes , { } , schema . fields )
271
286
. then ( ( ) => this . _schemaCollection ( ) )
272
- . then ( schemaCollection => schemaCollection . insertSchema ( mongoObject ) ) ;
287
+ . then ( schemaCollection => schemaCollection . insertSchema ( mongoObject ) )
288
+ . catch ( err => this . handleError ( err ) ) ;
273
289
}
274
290
275
291
addFieldIfNotExists ( className : string , fieldName : string , type : any ) : Promise < void > {
276
292
return this . _schemaCollection ( )
277
293
. then ( schemaCollection => schemaCollection . addFieldIfNotExists ( className , fieldName , type ) )
278
- . then ( ( ) => this . createIndexesIfNeeded ( className , fieldName , type ) ) ;
294
+ . then ( ( ) => this . createIndexesIfNeeded ( className , fieldName , type ) )
295
+ . catch ( err => this . handleError ( err ) ) ;
279
296
}
280
297
281
298
// Drops a collection. Resolves with true if it was a Parse Schema (eg. _User, Custom, etc.)
@@ -293,12 +310,14 @@ export class MongoStorageAdapter implements StorageAdapter {
293
310
// We've dropped the collection, now remove the _SCHEMA document
294
311
. then ( ( ) => this . _schemaCollection ( ) )
295
312
. then ( schemaCollection => schemaCollection . findAndDeleteSchema ( className ) )
313
+ . catch ( err => this . handleError ( err ) ) ;
296
314
}
297
315
298
316
// Delete all data known to this adapter. Used for testing.
299
317
deleteAllClasses ( ) {
300
318
return storageAdapterAllCollections ( this )
301
- . then ( collections => Promise . all ( collections . map ( collection => collection . drop ( ) ) ) ) ;
319
+ . then ( collections => Promise . all ( collections . map ( collection => collection . drop ( ) ) ) )
320
+ . catch ( err => this . handleError ( err ) ) ;
302
321
}
303
322
304
323
// Remove the column and all the data. For Relations, the _Join collection is handled
@@ -342,14 +361,16 @@ export class MongoStorageAdapter implements StorageAdapter {
342
361
return this . _adaptiveCollection ( className )
343
362
. then ( collection => collection . updateMany ( { } , collectionUpdate ) )
344
363
. then ( ( ) => this . _schemaCollection ( ) )
345
- . then ( schemaCollection => schemaCollection . updateSchema ( className , schemaUpdate ) ) ;
364
+ . then ( schemaCollection => schemaCollection . updateSchema ( className , schemaUpdate ) )
365
+ . catch ( err => this . handleError ( err ) ) ;
346
366
}
347
367
348
368
// Return a promise for all schemas known to this adapter, in Parse format. In case the
349
369
// schemas cannot be retrieved, returns a promise that rejects. Requirements for the
350
370
// rejection reason are TBD.
351
371
getAllClasses ( ) : Promise < StorageClass [ ] > {
352
- return this . _schemaCollection ( ) . then ( schemasCollection => schemasCollection . _fetchAllSchemasFrom_SCHEMA ( ) ) ;
372
+ return this . _schemaCollection ( ) . then ( schemasCollection => schemasCollection . _fetchAllSchemasFrom_SCHEMA ( ) )
373
+ . catch ( err => this . handleError ( err ) ) ;
353
374
}
354
375
355
376
// Return a promise for the schema with the given name, in Parse format. If
@@ -358,6 +379,7 @@ export class MongoStorageAdapter implements StorageAdapter {
358
379
getClass ( className : string ) : Promise < StorageClass > {
359
380
return this . _schemaCollection ( )
360
381
. then ( schemasCollection => schemasCollection . _fetchOneSchemaFrom_SCHEMA ( className ) )
382
+ . catch ( err => this . handleError ( err ) ) ;
361
383
}
362
384
363
385
// TODO: As yet not particularly well specified. Creates an object. Maybe shouldn't even need the schema,
@@ -381,7 +403,8 @@ export class MongoStorageAdapter implements StorageAdapter {
381
403
throw err ;
382
404
}
383
405
throw error ;
384
- } ) ;
406
+ } )
407
+ . catch ( err => this . handleError ( err ) ) ;
385
408
}
386
409
387
410
// Remove all objects that match the given Parse Query.
@@ -394,6 +417,7 @@ export class MongoStorageAdapter implements StorageAdapter {
394
417
const mongoWhere = transformWhere ( className , query , schema ) ;
395
418
return collection . deleteMany ( mongoWhere )
396
419
} )
420
+ . catch ( err => this . handleError ( err ) )
397
421
. then ( ( { result } ) => {
398
422
if ( result . n === 0 ) {
399
423
throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
@@ -410,7 +434,8 @@ export class MongoStorageAdapter implements StorageAdapter {
410
434
const mongoUpdate = transformUpdate ( className , update , schema ) ;
411
435
const mongoWhere = transformWhere ( className , query , schema ) ;
412
436
return this . _adaptiveCollection ( className )
413
- . then ( collection => collection . updateMany ( mongoWhere , mongoUpdate ) ) ;
437
+ . then ( collection => collection . updateMany ( mongoWhere , mongoUpdate ) )
438
+ . catch ( err => this . handleError ( err ) ) ;
414
439
}
415
440
416
441
// Atomically finds and updates an object based on query.
@@ -427,7 +452,8 @@ export class MongoStorageAdapter implements StorageAdapter {
427
452
throw new Parse . Error ( Parse . Error . DUPLICATE_VALUE , 'A duplicate value for a field with unique values was provided' ) ;
428
453
}
429
454
throw error ;
430
- } ) ;
455
+ } )
456
+ . catch ( err => this . handleError ( err ) ) ;
431
457
}
432
458
433
459
// Hopefully we can get rid of this. It's only used for config and hooks.
@@ -436,7 +462,8 @@ export class MongoStorageAdapter implements StorageAdapter {
436
462
const mongoUpdate = transformUpdate ( className , update , schema ) ;
437
463
const mongoWhere = transformWhere ( className , query , schema ) ;
438
464
return this . _adaptiveCollection ( className )
439
- . then ( collection => collection . upsertOne ( mongoWhere , mongoUpdate ) ) ;
465
+ . then ( collection => collection . upsertOne ( mongoWhere , mongoUpdate ) )
466
+ . catch ( err => this . handleError ( err ) ) ;
440
467
}
441
468
442
469
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
@@ -461,6 +488,7 @@ export class MongoStorageAdapter implements StorageAdapter {
461
488
readPreference,
462
489
} ) )
463
490
. then ( objects => objects . map ( object => mongoObjectToParseObject ( className , object , schema ) ) )
491
+ . catch ( err => this . handleError ( err ) ) ;
464
492
}
465
493
466
494
// Create a unique index. Unique indexes on nullable fields are not allowed. Since we don't
@@ -482,14 +510,15 @@ export class MongoStorageAdapter implements StorageAdapter {
482
510
throw new Parse . Error ( Parse . Error . DUPLICATE_VALUE , 'Tried to ensure field uniqueness for a class that already has duplicates.' ) ;
483
511
}
484
512
throw error ;
485
- } ) ;
513
+ } )
514
+ . catch ( err => this . handleError ( err ) ) ;
486
515
}
487
516
488
517
// Used in tests
489
518
_rawFind ( className : string , query : QueryType ) {
490
519
return this . _adaptiveCollection ( className ) . then ( collection => collection . find ( query , {
491
520
maxTimeMS : this . _maxTimeMS ,
492
- } ) ) ;
521
+ } ) ) . catch ( err => this . handleError ( err ) ) ;
493
522
}
494
523
495
524
// Executes a count.
@@ -500,7 +529,8 @@ export class MongoStorageAdapter implements StorageAdapter {
500
529
. then ( collection => collection . count ( transformWhere ( className , query , schema ) , {
501
530
maxTimeMS : this . _maxTimeMS ,
502
531
readPreference,
503
- } ) ) ;
532
+ } ) )
533
+ . catch ( err => this . handleError ( err ) ) ;
504
534
}
505
535
506
536
distinct ( className : string , schema : SchemaType , query : QueryType , fieldName : string ) {
@@ -520,7 +550,8 @@ export class MongoStorageAdapter implements StorageAdapter {
520
550
}
521
551
return mongoObjectToParseObject ( className , object , schema ) ;
522
552
} ) ;
523
- } ) ;
553
+ } )
554
+ . catch ( err => this . handleError ( err ) ) ;
524
555
}
525
556
526
557
aggregate ( className : string , schema : any , pipeline : any , readPreference : ?string ) {
@@ -573,7 +604,8 @@ export class MongoStorageAdapter implements StorageAdapter {
573
604
} ) ;
574
605
return results ;
575
606
} )
576
- . then ( objects => objects . map ( object => mongoObjectToParseObject ( className , object , schema ) ) ) ;
607
+ . then ( objects => objects . map ( object => mongoObjectToParseObject ( className , object , schema ) ) )
608
+ . catch ( err => this . handleError ( err ) ) ;
577
609
}
578
610
579
611
_parseReadPreference ( readPreference : ?string ) : ?string {
@@ -609,12 +641,14 @@ export class MongoStorageAdapter implements StorageAdapter {
609
641
610
642
createIndex ( className : string , index : any ) {
611
643
return this . _adaptiveCollection ( className )
612
- . then ( collection => collection . _mongoCollection . createIndex ( index ) ) ;
644
+ . then ( collection => collection . _mongoCollection . createIndex ( index ) )
645
+ . catch ( err => this . handleError ( err ) ) ;
613
646
}
614
647
615
648
createIndexes ( className : string , indexes : any ) {
616
649
return this . _adaptiveCollection ( className )
617
- . then ( collection => collection . _mongoCollection . createIndexes ( indexes ) ) ;
650
+ . then ( collection => collection . _mongoCollection . createIndexes ( indexes ) )
651
+ . catch ( err => this . handleError ( err ) ) ;
618
652
}
619
653
620
654
createIndexesIfNeeded ( className : string , fieldName : string , type : any ) {
@@ -656,17 +690,20 @@ export class MongoStorageAdapter implements StorageAdapter {
656
690
657
691
getIndexes ( className : string ) {
658
692
return this . _adaptiveCollection ( className )
659
- . then ( collection => collection . _mongoCollection . indexes ( ) ) ;
693
+ . then ( collection => collection . _mongoCollection . indexes ( ) )
694
+ . catch ( err => this . handleError ( err ) ) ;
660
695
}
661
696
662
697
dropIndex ( className : string , index : any ) {
663
698
return this . _adaptiveCollection ( className )
664
- . then ( collection => collection . _mongoCollection . dropIndex ( index ) ) ;
699
+ . then ( collection => collection . _mongoCollection . dropIndex ( index ) )
700
+ . catch ( err => this . handleError ( err ) ) ;
665
701
}
666
702
667
703
dropAllIndexes ( className : string ) {
668
704
return this . _adaptiveCollection ( className )
669
- . then ( collection => collection . _mongoCollection . dropIndexes ( ) ) ;
705
+ . then ( collection => collection . _mongoCollection . dropIndexes ( ) )
706
+ . catch ( err => this . handleError ( err ) ) ;
670
707
}
671
708
672
709
updateSchemaWithIndexes ( ) : Promise < any > {
@@ -676,7 +713,8 @@ export class MongoStorageAdapter implements StorageAdapter {
676
713
return this . setIndexesFromMongo ( schema . className ) ;
677
714
} ) ;
678
715
return Promise . all ( promises ) ;
679
- } ) ;
716
+ } )
717
+ . catch ( err => this . handleError ( err ) ) ;
680
718
}
681
719
}
682
720
0 commit comments