sails

编程入门 行业动态 更新时间:2024-10-17 09:54:55
sails-mongo适配器,规范化错误消息(sails-mongo adapter, normalize error messages) mongodb

我正在使用sails-mongo适配器尝试使用mongodb的sailsJs。 将验证添加到模型后,验证失败时会收到以下响应。

Users.js型号:

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

使用sails-mongo适配器时验证错误:

{ "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\" }" } } }

如果我使用开发数据库(sails-disk adapter),我会得到更好的格式化响应。

使用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`)." } ] } } }

作为开发人员,我希望从框架中获得标准化的响应,任何人都可以帮助我以优雅的方式处理此类验证错误。 我的意思是我不能"E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }"显示错误"E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }" 。

谢谢。

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.

最满意答案

sails.js只是报告数据库给出的错误。 这就是sails-disk有更好的错误消息的情况。 sails-mongo适配器最终会为您提供数据库直接报告的错误; 所以要美化这些,你只需要将原始错误映射到更加用户友好的消息,就像任何其他数据库驱动程序一样。

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.

更多推荐

本文发布于:2023-07-14 14:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:sails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!