gpt4 book ai didi

javascript - 发送明确的错误消息作为响应

转载 作者:行者123 更新时间:2023-12-03 08:42:18 26 4
gpt4 key购买 nike

这是 Sequelize 请求 :

models.client
.create(clientInformation)
.then(clientCreated => {
logger.info("clientCreated", JSON.stringify(clientCreated));
res.status(200).json(clientCreated);
})
.catch(error => {
logger.error("error", JSON.stringify(error));
res.status(400).json(error);
});

这是 客户端模型
module.exports = function(sequelize, DataTypes) {
var client = sequelize.define(
"client",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true,
unique: true
},
nomClient: DataTypes.STRING,
numTel: {
type: DataTypes.INTEGER,
unique: true
},
email: {
type: DataTypes.STRING,
unique: true
},
addresse: {
type: DataTypes.STRING,
unique: true
},
ville: DataTypes.STRING
},
{ timestamps: true }
);

return client;
};

我故意在 req.body 中发送无效数据以实现明确的错误处理。所以在 响应中, 我得到了这个:
{
"name": "SequelizeDatabaseError",
"parent": {
"name": "error",
"length": 177,
"severity": "ERROR",
"code": "22003",
"file": "d:\\pginstaller_12.auto\\postgres.windows-x64\\src\\backend\\utils\\adt\\numutils.c",
"line": "253",
"routine": "pg_strtoint32",
"sql": "INSERT INTO \"clients\" (\"id\",\"nomClient\",\"numTel\",\"email\",\"addresse\",\"ville\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;",
"parameters": [
"Sam",
"5578757867",
"eazfafe@gmail.com",
"Nabeul",
"RueDuMerle",
"2020-03-13 08:53:41.214 +00:00",
"2020-03-13 08:53:41.214 +00:00"
]
},
"original": {
"name": "error",
"length": 177,
"severity": "ERROR",
"code": "22003",
"file": "d:\\pginstaller_12.auto\\postgres.windows-x64\\src\\backend\\utils\\adt\\numutils.c",
"line": "253",
"routine": "pg_strtoint32",
"sql": "INSERT INTO \"clients\" (\"id\",\"nomClient\",\"numTel\",\"email\",\"addresse\",\"ville\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;",
"parameters": [
"Sam",
"5578757867",
"eazfafe@gmail.com",
"Nabeul",
"RueDuMerle",
"2020-03-13 08:53:41.214 +00:00",
"2020-03-13 08:53:41.214 +00:00"
]
},
"sql": "INSERT INTO \"clients\" (\"id\",\"nomClient\",\"numTel\",\"email\",\"addresse\",\"ville\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;",
"parameters": [
"Sam",
"5578757867",
"eazfafe@gmail.com",
"Nabeul",
"RueDuMerle",
"2020-03-13 08:53:41.214 +00:00",
"2020-03-13 08:53:41.214 +00:00"
]
}

记录的错误中, 是这样的:
DatabaseError [SequelizeDatabaseError]: value "5578757867" is out of range for type integer
at Query.formatError (C:\work\ViwoneSavServer\Backend\node_modules\sequelize\lib\dialects\postgres\query.js:366:16)
at C:\work\ViwoneSavServer\Backend\node_modules\sequelize\lib\dialects\postgres\query.js:72:18
at tryCatcher (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\promise.js:725:18)
at _drainQueueStep (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:\work\ViwoneSavServer\Backend\node_modules\bluebird\js\release\async.js:15:14)

我的问题是记录的消息中存在的信息:

value "5578757867" is out of range for type integer



响应体 中无处可寻!
所以这里是我的问题:
问题 1: 为什么记录的错误与响应中的错误不同,尽管它们是同一个对象?
问题 2: 我如何只访问错误消息并在响应中发送它?
注意: SO 中关于 sequelize 错误处理的可用答案不提供对此的响应。
例如,大部分答案是这样的:
sequelize.create({ 
name: req.body.name,
email: req.body.email })
.then((data)=> { res.send('mysuccess message'); })
.catch(Sequelize.UniqueConstraintError => {
res.send('myerrormessage');
}));

换句话说,我是否需要检查每种错误类型?

最佳答案

我知道这个问题已经有一段时间了,但我偶然发现了同样的问题,并通过使用 error.message 而不是 error 从 Sequelize 获取确切的错误消息来解决它。所以你的代码会变成

models.client
.create(clientInformation)
.then(clientCreated => {
logger.info("clientCreated", JSON.stringify(clientCreated));
res.status(200).json(clientCreated);
})
.catch(error => {
logger.error("error", JSON.stringify(error.message));
res.status(400).json(error.message);
});

关于javascript - 发送明确的错误消息作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com