From 030147207c9a8c8c1e8c989da76089964e763d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kartal=20Kaan=20Bozdo=C4=9Fan?= Date: Tue, 8 Mar 2022 11:57:21 +0300 Subject: [PATCH] Also use INVALID_VALUE for object updates --- spec/ParseAPI.spec.js | 11 +++- .../Storage/Mongo/MongoStorageAdapter.js | 54 +++++++++---------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/spec/ParseAPI.spec.js b/spec/ParseAPI.spec.js index ea6563b5ff..0add04058e 100644 --- a/spec/ParseAPI.spec.js +++ b/spec/ParseAPI.spec.js @@ -48,7 +48,7 @@ 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(); @@ -56,6 +56,15 @@ describe_only_db('mongo')('spatial index', () => { } 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 () => { diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index f6b7a838d1..e16162b1df 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -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; @@ -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)); } @@ -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)); }