Skip to content

Commit

Permalink
Also use INVALID_VALUE for object updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mstniy committed Mar 8, 2022
1 parent a9aad2e commit 0301472
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
11 changes: 10 additions & 1 deletion spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ describe_only_db('mongo')('spatial index', () => {
});

it('invalid geometry fails (#7331)', async () => {
const obj = new Parse.Object('geojson_test');
let obj = new Parse.Object('geojson_test');
obj.set('geometry', { foo: 'bar' });
try {
await obj.save();
fail('Invalid geometry did not fail');
} catch (e) {
expect(e.code).toEqual(Parse.Error.INVALID_VALUE);
}
obj = new Parse.Object('geojson_test');
await obj.save();
try {
obj.set('geometry', { foo: 'bar' });
await obj.save();
fail('Invalid geometry did not fail');
} catch (e) {
expect(e.code).toEqual(Parse.Error.INVALID_VALUE);
}
});

it('valid geometry succeeds', async () => {
Expand Down
54 changes: 26 additions & 28 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ function validateExplainValue(explain) {
}
}

function mongoErrorToParse(error) {
if (error.code === 11000) {
// Duplicate value
const err = new Parse.Error(
Parse.Error.DUPLICATE_VALUE,
'A duplicate value for a field with unique values was provided'
);
err.underlyingError = error;
if (error.message) {
const matches = error.message.match(/index:[\sa-zA-Z0-9_\-\.]+\$?([a-zA-Z_-]+)_1/);
if (matches && Array.isArray(matches)) {
err.userInfo = { duplicated_field: matches[1] };
}
}
return err;
} else if (error.code === 16755 || error.code === 16756) {
// Can't extract geo keys
const err = new Parse.Error(Parse.Error.INVALID_VALUE, error.message);
err.underlyingError = error;
return err;
}
return error;
}

export class MongoStorageAdapter implements StorageAdapter {
// Private
_uri: string;
Expand Down Expand Up @@ -481,27 +505,7 @@ export class MongoStorageAdapter implements StorageAdapter {
.then(collection => collection.insertOne(mongoObject, transactionalSession))
.then(() => ({ ops: [mongoObject] }))
.catch(error => {
if (error.code === 11000) {
// Duplicate value
const err = new Parse.Error(
Parse.Error.DUPLICATE_VALUE,
'A duplicate value for a field with unique values was provided'
);
err.underlyingError = error;
if (error.message) {
const matches = error.message.match(/index:[\sa-zA-Z0-9_\-\.]+\$?([a-zA-Z_-]+)_1/);
if (matches && Array.isArray(matches)) {
err.userInfo = { duplicated_field: matches[1] };
}
}
throw err;
} else if (error.code === 16755 || error.code === 16756) {
// Can't extract geo keys
const err = new Parse.Error(Parse.Error.INVALID_VALUE, error.message);
err.underlyingError = error;
throw err;
}
throw error;
throw mongoErrorToParse(error);
})
.catch(err => this.handleError(err));
}
Expand Down Expand Up @@ -572,13 +576,7 @@ export class MongoStorageAdapter implements StorageAdapter {
)
.then(result => mongoObjectToParseObject(className, result.value, schema))
.catch(error => {
if (error.code === 11000) {
throw new Parse.Error(
Parse.Error.DUPLICATE_VALUE,
'A duplicate value for a field with unique values was provided'
);
}
throw error;
throw mongoErrorToParse(error);
})
.catch(err => this.handleError(err));
}
Expand Down

0 comments on commit 0301472

Please sign in to comment.