Skip to content

Return correct error when violating unique index #1229

@milesrichardson

Description

@milesrichardson

For implementation related questions or technical support, please refer to the Stack Overflow and Server Fault communities.

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

Environment Setup

  • parse-server latest running in docker on gcloud compute server
  • mongolab instance with unique constraint on column of a table

Steps to reproduce

(I've only tested this with columns of type Number, but I imagine it happens for every column type)

  1. Create class SomeClass with Number column some_column
  2. In Mongo, set a unique index on SomeClass.some_column
  3. Save a row to SomeClass with some_column=123
  4. Attempt to save another row to SomeClass with some_column=123 via the REST_API

In this case, parse-server will return the following error:

{"code":1,"message":"Internal server error."}

Whereas hosted parse will return a much more helpful error message:

{"code":137,"error":"A duplicate value for a field with unique values was provided"}

I'm calling this a "minor" issue because although the row will (correctly) fail to save, any code relying on parsing error messages (e.g. for retry logic) will break.

Interestingly, this ONLY happens when saving a SINGLE duplicate row via the REST API. When using batch operations (https://parse.com/docs/rest/guide#objects-batch-operations) to save multiple duplicate rows, or one duplicate row and one/multiple unique rows, parse-server correctly (?) returns the following message, and successfully saves the non-duplicate rows:

{"code": 11000, "error": "E11000 duplicate key error index: myapp.SomeClass.$some_column_1 dup key: { : 123 }"}

In this batch case, there is no error in the parse-server logs. However, in the saving single row case, there is an error in the parse-server logs (see below).

Logs/Trace

parse server running on http://localhost:1337/parse
Uncaught internal server error. { [MongoError: E11000 duplicate key error index: myapp.SomeClass.$some_column_1 dup key: { : 123 }]
  name: 'MongoError',
  message: 'E11000 duplicate key error index: myapp.SomeClass.$some_column_1 dup key: { : 123 }',
  driver: true,
  index: 0,
  code: 11000,
  errmsg: 'E11000 duplicate key error index: myapp.SomeClass.$some_column_1 dup key: { : 123 }' } MongoError: E11000 duplicate key error index: myapp.SomeClass.$some_column_1 dup key: { : 123 }
    at Function.MongoError.create (/parse/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/parse/node_modules/mongodb/lib/utils.js:114:22)
    at /parse/node_modules/mongodb/lib/collection.js:701:67
    at handler (/parse/node_modules/mongodb-core/lib/topologies/replset.js:400:5)
    at commandCallback (/parse/node_modules/mongodb-core/lib/topologies/server.js:1143:9)
    at Callbacks.emit (/parse/node_modules/mongodb-core/lib/topologies/server.js:116:3)
    at null.messageHandler (/parse/node_modules/mongodb-core/lib/topologies/server.js:291:23)
    at Socket.<anonymous> (/parse/node_modules/mongodb-core/lib/connection/connection.js:285:22)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions