@@ -125,6 +125,30 @@ function validateExplainValue(explain) {
125
125
}
126
126
}
127
127
128
+ function mongoErrorToParse ( error ) {
129
+ if ( error . code === 11000 ) {
130
+ // Duplicate value
131
+ const err = new Parse . Error (
132
+ Parse . Error . DUPLICATE_VALUE ,
133
+ 'A duplicate value for a field with unique values was provided'
134
+ ) ;
135
+ err . underlyingError = error ;
136
+ if ( error . message ) {
137
+ const matches = error . message . match ( / i n d e x : [ \s a - z A - Z 0 - 9 _ \- \. ] + \$ ? ( [ a - z A - Z _ - ] + ) _ 1 / ) ;
138
+ if ( matches && Array . isArray ( matches ) ) {
139
+ err . userInfo = { duplicated_field : matches [ 1 ] } ;
140
+ }
141
+ }
142
+ return err ;
143
+ } else if ( error . code === 16755 || error . code === 16756 ) {
144
+ // Can't extract geo keys
145
+ const err = new Parse . Error ( Parse . Error . INVALID_VALUE , error . message ) ;
146
+ err . underlyingError = error ;
147
+ return err ;
148
+ }
149
+ return error ;
150
+ }
151
+
128
152
export class MongoStorageAdapter implements StorageAdapter {
129
153
// Private
130
154
_uri : string ;
@@ -481,27 +505,7 @@ export class MongoStorageAdapter implements StorageAdapter {
481
505
. then ( collection => collection . insertOne ( mongoObject , transactionalSession ) )
482
506
. then ( ( ) => ( { ops : [ mongoObject ] } ) )
483
507
. catch ( error => {
484
- if ( error . code === 11000 ) {
485
- // Duplicate value
486
- const err = new Parse . Error (
487
- Parse . Error . DUPLICATE_VALUE ,
488
- 'A duplicate value for a field with unique values was provided'
489
- ) ;
490
- err . underlyingError = error ;
491
- if ( error . message ) {
492
- const matches = error . message . match ( / i n d e x : [ \s a - z A - Z 0 - 9 _ \- \. ] + \$ ? ( [ a - z A - Z _ - ] + ) _ 1 / ) ;
493
- if ( matches && Array . isArray ( matches ) ) {
494
- err . userInfo = { duplicated_field : matches [ 1 ] } ;
495
- }
496
- }
497
- throw err ;
498
- } else if ( error . code === 16755 || error . code === 16756 ) {
499
- // Can't extract geo keys
500
- const err = new Parse . Error ( Parse . Error . INVALID_VALUE , error . message ) ;
501
- err . underlyingError = error ;
502
- throw err ;
503
- }
504
- throw error ;
508
+ throw mongoErrorToParse ( error ) ;
505
509
} )
506
510
. catch ( err => this . handleError ( err ) ) ;
507
511
}
@@ -572,13 +576,7 @@ export class MongoStorageAdapter implements StorageAdapter {
572
576
)
573
577
. then ( result => mongoObjectToParseObject ( className , result . value , schema ) )
574
578
. catch ( error => {
575
- if ( error . code === 11000 ) {
576
- throw new Parse . Error (
577
- Parse . Error . DUPLICATE_VALUE ,
578
- 'A duplicate value for a field with unique values was provided'
579
- ) ;
580
- }
581
- throw error ;
579
+ throw mongoErrorToParse ( error ) ;
582
580
} )
583
581
. catch ( err => this . handleError ( err ) ) ;
584
582
}
0 commit comments