Skip to content

Commit fba80c8

Browse files
committed
Also use INVALID_VALUE for object updates
1 parent a9aad2e commit fba80c8

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

spec/ParseAPI.spec.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,24 @@ describe_only_db('mongo')('spatial index', () => {
4747
await testSchema.save();
4848
});
4949

50-
it('invalid geometry fails (#7331)', async () => {
51-
const obj = new Parse.Object('geojson_test');
50+
fit('invalid geometry fails (#7331)', async () => {
51+
let obj = new Parse.Object('geojson_test');
5252
obj.set('geometry', { foo: 'bar' });
5353
try {
5454
await obj.save();
5555
fail('Invalid geometry did not fail');
5656
} catch (e) {
5757
expect(e.code).toEqual(Parse.Error.INVALID_VALUE);
5858
}
59+
obj = new Parse.Object('geojson_test');
60+
await obj.save();
61+
try {
62+
obj.set('geometry', { foo: 'bar' });
63+
await obj.save();
64+
fail('Invalid geometry did not fail');
65+
} catch (e) {
66+
expect(e.code).toEqual(Parse.Error.INVALID_VALUE);
67+
}
5968
});
6069

6170
it('valid geometry succeeds', async () => {

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+26-28
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,30 @@ function validateExplainValue(explain) {
125125
}
126126
}
127127

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(/index:[\sa-zA-Z0-9_\-\.]+\$?([a-zA-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+
128152
export class MongoStorageAdapter implements StorageAdapter {
129153
// Private
130154
_uri: string;
@@ -481,27 +505,7 @@ export class MongoStorageAdapter implements StorageAdapter {
481505
.then(collection => collection.insertOne(mongoObject, transactionalSession))
482506
.then(() => ({ ops: [mongoObject] }))
483507
.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(/index:[\sa-zA-Z0-9_\-\.]+\$?([a-zA-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);
505509
})
506510
.catch(err => this.handleError(err));
507511
}
@@ -572,13 +576,7 @@ export class MongoStorageAdapter implements StorageAdapter {
572576
)
573577
.then(result => mongoObjectToParseObject(className, result.value, schema))
574578
.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);
582580
})
583581
.catch(err => this.handleError(err));
584582
}

0 commit comments

Comments
 (0)