如何创建Node.js自定义错误并在res.json()后返回堆栈属性 [英] How to create Node.js custom errors and have the stack property return after res.json()

查看:82
本文介绍了如何创建Node.js自定义错误并在res.json()后返回堆栈属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Node中创建自定义错误对象,使用Error作为原型,然后通过快递 res.json()将这些错误返回给客户端方法。我可以创建一个自定义错误的实例,设置它的属性但是当我调用 res.json()时,堆栈属性丢失了。我相信这是由于 JSON.stringify()不按原型?

I’m attempting to create custom error objects in Node that use Error as the prototype and then to return these errors to the client via the express res.json() method. I can create an instance of a custom error, set it’s properties but when I call res.json(), the stack property is missing. I believe this is due to JSON.stringify() not following prototypes?

我不明白的方式具有属性的任意数量的创建自定义错误的对象,然后使用的 JSON.stringify()在这些对象返回的所有属性和基类/超错误堆栈属性。请帮忙!下面是一个例子:

I can’t see a way of creating custom error objects with an arbitrary number of properties and then use JSON.stringify() on these objects to return all the properties and the base class / superclass Error stack property. Please help! An example is below:

(function (customErrorController) {


    function CustomError() {

        this.name = 'CustomError'; // String
        this.message = null; // String
        this.errorCode = null; // String
        // this.stack exists as CustomError inherits from Error
    }

    CustomError.prototype = new Error();


    customErrorController.init = function (app) {

        app.get('/test', function (req, res) {

            var customError = new CustomError();
            customError.message = 'The Message';
            customError.errorCode = 'Err001';

            console.log(customError.stack); // This logs the stack fine

            res.json(customError); // The stack is missing when outputting the object as JSON

        });
    };

})(module.exports);


推荐答案

感谢您的评论。从这些和更多的挖掘,我现在使用以下代码创建自定义错误并在调用 res.json()带错误:

Thanks for the comments. From these and from a bit more digging, I’m now using the following code to create custom errors and have the correct stack on the stack property return after calling res.json() with the error:

// Redefine properties on Error to be enumerable
Object.defineProperty(Error.prototype, 'message', { configurable: true, enumerable: true });
Object.defineProperty(Error.prototype, 'stack', { configurable: true, enumerable: true });


(function (customErrorController) {

    function CustomError(message, errorCode) {

        Error.captureStackTrace(this, CustomError);

        this.name = 'CustomError'; // String
        this.message = message; // String
        this.errorCode = errorCode; // String
    }

    CustomError.prototype = Object.create(Error.prototype);

    customErrorController.init = function (app) {

        app.get('/test', function (req, res) {

            var customError = new CustomError('The Message', 'Err001');
            res.json(customError); // The stack property is now included

        });
    };

})(module.exports);

这篇关于如何创建Node.js自定义错误并在res.json()后返回堆栈属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆