3

I am trying out sailsJs with mongodb using the sails-mongo adapter. After adding validations to a model, I get the following response when the validation fails.

Users.js Model:

module.exports = {
    schema: true,
    attributes: {
        name: {
            type: "string",
            unique: true
        },
        email: {
            type: "email",
            unique: true
        },
        password: {
            type: "string",
            required: true
        }
    }
}   

Validation error while using sails-mongo adapter:

{
  "error": {
    "error": "E_UNKNOWN",
    "status": 500,
    "summary": "Encountered an unexpected error",
    "raw": {
      "name": "MongoError",
      "code": 11000,
      "err": "E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }"
    }
  }
}

I get a better formatted response if I use the in development database which is the sails-disk adapter.

Validation error while using sails-disk adapter:

{
  "error": {
    "error": "E_VALIDATION",
    "status": 400,
    "summary": "2 attributes are invalid",
    "invalidAttributes": {
      "name": [
        {
          "value": "codin",
          "rule": "unique",
          "message": "A record with that `name` already exists (`codin`)."
        }
      ]
    }
  }
}

As a developer, I would expect a standardized response from a framework, Can anyone help me with a graceful way of handling such validation errors. I mean I cannot just show the error "E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }" to a layman user.

Thanks.

hussainb
  • 1,218
  • 2
  • 15
  • 33

1 Answers1

1

sails.js is just reporting the error given by the database. It's just the case that sails-disk has nicer error messages. The sails-mongo adapter ends up giving you the error that's reported directly by the database; so to prettify these, you'd just need to map the raw errors into more user-friendly messages just like any other database driver.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • 1
    Right, I understood that a little late, I am going to do what you said by mapping the error CODES reported by mongo to some error messages, does that sound ok or are you aware of some other method, Thanks – hussainb Apr 04 '15 at 17:43
  • This also happens with sails-postgres in my case 0.11.2 – jhonny lopez Feb 09 '16 at 04:12