gpt4 book ai didi

node.js - 如何抛出我自己的不显示调用堆栈的错误?

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:40 25 4
gpt4 key购买 nike

我有一个带有 2 个 JavaScript 文件的简单应用程序。如何抛出我自己的安全错误(来自 service.js 的 insertorupdate),以便不向用户透露调用堆栈?

console.js

const contracts = require('./contractService');

(async () => {

let contractToUpdate = {
idContract: 102,
AccountNo_Lender: 'Back to resolve'
};

try {
var results2 = await contracts.update(contractToUpdate);
console.log(results2);
} catch (error) {
console.log(error);
}

})();

contractService.js

require('dotenv/config');
const Sequelize = require('sequelize');

const sequelize = new Sequelize(process.env.DATABASE, process.env.USER, process.env.PASSWORD, {
host: process.env.HOST,
dialect: 'mysql',
operatorsAliases: false,
define: {
timestamps: false,
freezeTableName: true
},

pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}

});

sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});

/**
* Model for the contract entity.
*/
const Contract = sequelize.define('Contract', {
idContract: {
type: Sequelize.INTEGER,
primaryKey: true
},
AccountNo_Lender: {
type: Sequelize.STRING,
allowNull: false
}
});

exports.update = function (contract) {
return new Promise(function (resolve, reject) {
Contract.insertOrUpdate(contract) <====== Right here don't throw call stack
.then(c => {
resolve(c);
});
});
}

最佳答案

为了避免显示一般的调用堆栈,只需执行以下操作:

function someFunction()
{
throw new Error("Something bad happened");
}

try
{
someFunction();
}
catch (err)
{
console.error("Error message: " + err.message);
}

关于node.js - 如何抛出我自己的不显示调用堆栈的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50988407/

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