Giter Club home page Giter Club logo

Comments (7)

mattinsler avatar mattinsler commented on September 25, 2024

You can use https://npmjs.org/package/json-stringify-safe to stringify the error and pass that into res.json.

from longjohn.

binarykitchen avatar binarykitchen commented on September 25, 2024

Mmmmhhh, thanks for that. Why is Express not using it then?

from longjohn.

mattinsler avatar mattinsler commented on September 25, 2024

It would add some overhead to every single response that uses res.json, which express can't assume a user would be willing to take on. It just doesn't make sense for the framework to make those types of choices for users.

from longjohn.

binarykitchen avatar binarykitchen commented on September 25, 2024

Makes sense, thx

from longjohn.

binarykitchen avatar binarykitchen commented on September 25, 2024

I found a better way without adding much overhead nor an external module. I modified the toJSON method like that:

Object.defineProperty(Error.prototype, 'toJSON', {
    value: function() {
        var json = {};

        Object.getOwnPropertyNames(this).forEach(function(key) {
            // ignore long john stuff like '__cached_trace__'
            if (!/__(.)+__/.test(key)) {
                json[key] = this[key];
            }
        }, this);

        return json;
    },
    configurable: true
});

from longjohn.

mattinsler avatar mattinsler commented on September 25, 2024

This definitely works. Be careful when augmenting the core prototypes. You're better off usually adding a method, like toCleanJSON or something like that so that the core type still operates as expected by any other code that may utilize it.

from longjohn.

binarykitchen avatar binarykitchen commented on September 25, 2024

@mattinsler Agree with you. I just changed that into toCleanJSON in my code.

from longjohn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.