@@ -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,22 +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
- }
499
- throw error ;
508
+ throw mongoErrorToParse ( error ) ;
500
509
} )
501
510
. catch ( err => this . handleError ( err ) ) ;
502
511
}
@@ -567,13 +576,7 @@ export class MongoStorageAdapter implements StorageAdapter {
567
576
)
568
577
. then ( result => mongoObjectToParseObject ( className , result . value , schema ) )
569
578
. catch ( error => {
570
- if ( error . code === 11000 ) {
571
- throw new Parse . Error (
572
- Parse . Error . DUPLICATE_VALUE ,
573
- 'A duplicate value for a field with unique values was provided'
574
- ) ;
575
- }
576
- throw error ;
579
+ throw mongoErrorToParse ( error ) ;
577
580
} )
578
581
. catch ( err => this . handleError ( err ) ) ;
579
582
}
0 commit comments