Skip to content

Commit ec1f80d

Browse files
authored
Merge 0301472 into 8a126fc
2 parents 8a126fc + 0301472 commit ec1f80d

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

spec/ParseAPI.spec.js

+39
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,45 @@ describe('miscellaneous', () => {
3535
});
3636
});
3737

38+
describe_only_db('mongo')('spatial index', () => {
39+
beforeAll(async () => {
40+
await TestUtils.destroyAllDataPermanently(false);
41+
// Create a schema with a spatial index
42+
const testSchema = new Parse.Schema('geojson_test');
43+
testSchema.addObject('geometry');
44+
testSchema.addIndex('geospatial_index', {
45+
geometry: '2dsphere',
46+
});
47+
await testSchema.save();
48+
});
49+
50+
it('invalid geometry fails (#7331)', async () => {
51+
let obj = new Parse.Object('geojson_test');
52+
obj.set('geometry', { foo: 'bar' });
53+
try {
54+
await obj.save();
55+
fail('Invalid geometry did not fail');
56+
} catch (e) {
57+
expect(e.code).toEqual(Parse.Error.INVALID_VALUE);
58+
}
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+
}
68+
});
69+
70+
it('valid geometry succeeds', async () => {
71+
const obj = new Parse.Object('geojson_test');
72+
obj.set('geometry', { type: 'Point', coordinates: [-73.97, 40.77] });
73+
await obj.save();
74+
});
75+
});
76+
3877
describe('miscellaneous', function () {
3978
it('create a GameScore object', function (done) {
4079
const obj = new Parse.Object('GameScore');

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+26-23
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,22 +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-
}
499-
throw error;
508+
throw mongoErrorToParse(error);
500509
})
501510
.catch(err => this.handleError(err));
502511
}
@@ -567,13 +576,7 @@ export class MongoStorageAdapter implements StorageAdapter {
567576
)
568577
.then(result => mongoObjectToParseObject(className, result.value, schema))
569578
.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);
577580
})
578581
.catch(err => this.handleError(err));
579582
}

0 commit comments

Comments
 (0)