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

标签 node.js sequelize.js

我有一个带有 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/

相关文章:

node.js - 如何在 React App 中使用 Active Directory 实现 SSO 身份验证?

javascript - Sequelize-auto 无法连接到命名的 MSSQL 实例

enums - Sequelize.js 从现有数组中添加枚举

mysql - 如何在sequelize中使用mysql date_add函数?

postgresql - Sequelize Many to Many Include 失败,列不存在

node.js - Angular : Receive responses in order with the calls

php - 从 PHP 运行 node.js 脚本时无法访问文件

node.js - 使用 es7 async/await 检查 mongodb 中是否存在文档

javascript - Article.createdAt.toLocalDateString 不是函数

sql - 如何在 Sequelize 中逃脱?