Hide JSON Parse Errors in Express POST










-1















I have this little Express server:



var express = require("express");

var app = express();
app.use(express.json());

app.post("/hackme", (req, res) =>

if(!req.body.foo)
return res.send( error: 'oh no' );


return res.send( lbxapi: '1.1' );
);

app.listen(3050, function()
console.log("Express running");
);


When I send an invalid JSON POST body to the following server, I the full error stack like:



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected string in JSON at position 18
<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)
<br> &nbsp; &nbsp;at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> &nbsp; &nbsp;at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> &nbsp; &nbsp;at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> &nbsp; &nbsp;at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)
<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1091:14)
<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:174:19)
</pre>
</body>
</html>


I dont want to user to see this. How can I hide it? An answer would be accepted that changes my server's code so that I dont see it anymore. Thank you!










share|improve this question






















  • Show us the part of your code where you used JSON.parse

    – Badis Merabet
    Nov 15 '18 at 18:56











  • Check the default error handler of express expressjs.com/en/guide/error-handling.html

    – Ariel Alvarado
    Nov 15 '18 at 18:57











  • Have you considered using the process.env variable?

    – Samuel
    Nov 15 '18 at 19:11















-1















I have this little Express server:



var express = require("express");

var app = express();
app.use(express.json());

app.post("/hackme", (req, res) =>

if(!req.body.foo)
return res.send( error: 'oh no' );


return res.send( lbxapi: '1.1' );
);

app.listen(3050, function()
console.log("Express running");
);


When I send an invalid JSON POST body to the following server, I the full error stack like:



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected string in JSON at position 18
<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)
<br> &nbsp; &nbsp;at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> &nbsp; &nbsp;at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> &nbsp; &nbsp;at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> &nbsp; &nbsp;at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)
<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1091:14)
<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:174:19)
</pre>
</body>
</html>


I dont want to user to see this. How can I hide it? An answer would be accepted that changes my server's code so that I dont see it anymore. Thank you!










share|improve this question






















  • Show us the part of your code where you used JSON.parse

    – Badis Merabet
    Nov 15 '18 at 18:56











  • Check the default error handler of express expressjs.com/en/guide/error-handling.html

    – Ariel Alvarado
    Nov 15 '18 at 18:57











  • Have you considered using the process.env variable?

    – Samuel
    Nov 15 '18 at 19:11













-1












-1








-1








I have this little Express server:



var express = require("express");

var app = express();
app.use(express.json());

app.post("/hackme", (req, res) =>

if(!req.body.foo)
return res.send( error: 'oh no' );


return res.send( lbxapi: '1.1' );
);

app.listen(3050, function()
console.log("Express running");
);


When I send an invalid JSON POST body to the following server, I the full error stack like:



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected string in JSON at position 18
<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)
<br> &nbsp; &nbsp;at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> &nbsp; &nbsp;at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> &nbsp; &nbsp;at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> &nbsp; &nbsp;at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)
<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1091:14)
<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:174:19)
</pre>
</body>
</html>


I dont want to user to see this. How can I hide it? An answer would be accepted that changes my server's code so that I dont see it anymore. Thank you!










share|improve this question














I have this little Express server:



var express = require("express");

var app = express();
app.use(express.json());

app.post("/hackme", (req, res) =>

if(!req.body.foo)
return res.send( error: 'oh no' );


return res.send( lbxapi: '1.1' );
);

app.listen(3050, function()
console.log("Express running");
);


When I send an invalid JSON POST body to the following server, I the full error stack like:



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected string in JSON at position 18
<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)
<br> &nbsp; &nbsp;at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> &nbsp; &nbsp;at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> &nbsp; &nbsp;at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> &nbsp; &nbsp;at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)
<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1091:14)
<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:174:19)
</pre>
</body>
</html>


I dont want to user to see this. How can I hide it? An answer would be accepted that changes my server's code so that I dont see it anymore. Thank you!







javascript node.js json express






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 18:54









user3601578user3601578

5021517




5021517












  • Show us the part of your code where you used JSON.parse

    – Badis Merabet
    Nov 15 '18 at 18:56











  • Check the default error handler of express expressjs.com/en/guide/error-handling.html

    – Ariel Alvarado
    Nov 15 '18 at 18:57











  • Have you considered using the process.env variable?

    – Samuel
    Nov 15 '18 at 19:11

















  • Show us the part of your code where you used JSON.parse

    – Badis Merabet
    Nov 15 '18 at 18:56











  • Check the default error handler of express expressjs.com/en/guide/error-handling.html

    – Ariel Alvarado
    Nov 15 '18 at 18:57











  • Have you considered using the process.env variable?

    – Samuel
    Nov 15 '18 at 19:11
















Show us the part of your code where you used JSON.parse

– Badis Merabet
Nov 15 '18 at 18:56





Show us the part of your code where you used JSON.parse

– Badis Merabet
Nov 15 '18 at 18:56













Check the default error handler of express expressjs.com/en/guide/error-handling.html

– Ariel Alvarado
Nov 15 '18 at 18:57





Check the default error handler of express expressjs.com/en/guide/error-handling.html

– Ariel Alvarado
Nov 15 '18 at 18:57













Have you considered using the process.env variable?

– Samuel
Nov 15 '18 at 19:11





Have you considered using the process.env variable?

– Samuel
Nov 15 '18 at 19:11












1 Answer
1






active

oldest

votes


















0














Thanks to the comment by Ariel Alvarado (= using default error handler), I can answer my own question:



var express = require("express");

var app = express();
app.use(express.json());

// added this error handler
app.use(function (err, req, res, next)
console.error(err.stack)
res.status(500).send('Something broke!')
);

app.post("/hackme", (req, res) =>

if(!req.body.foo)
return res.send( error: 'oh no' );


return res.send( lbxapi: '1.1' );
);

app.listen(3050, function()
console.log("Express running");
);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326179%2fhide-json-parse-errors-in-express-post%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks to the comment by Ariel Alvarado (= using default error handler), I can answer my own question:



    var express = require("express");

    var app = express();
    app.use(express.json());

    // added this error handler
    app.use(function (err, req, res, next)
    console.error(err.stack)
    res.status(500).send('Something broke!')
    );

    app.post("/hackme", (req, res) =>

    if(!req.body.foo)
    return res.send( error: 'oh no' );


    return res.send( lbxapi: '1.1' );
    );

    app.listen(3050, function()
    console.log("Express running");
    );





    share|improve this answer



























      0














      Thanks to the comment by Ariel Alvarado (= using default error handler), I can answer my own question:



      var express = require("express");

      var app = express();
      app.use(express.json());

      // added this error handler
      app.use(function (err, req, res, next)
      console.error(err.stack)
      res.status(500).send('Something broke!')
      );

      app.post("/hackme", (req, res) =>

      if(!req.body.foo)
      return res.send( error: 'oh no' );


      return res.send( lbxapi: '1.1' );
      );

      app.listen(3050, function()
      console.log("Express running");
      );





      share|improve this answer

























        0












        0








        0







        Thanks to the comment by Ariel Alvarado (= using default error handler), I can answer my own question:



        var express = require("express");

        var app = express();
        app.use(express.json());

        // added this error handler
        app.use(function (err, req, res, next)
        console.error(err.stack)
        res.status(500).send('Something broke!')
        );

        app.post("/hackme", (req, res) =>

        if(!req.body.foo)
        return res.send( error: 'oh no' );


        return res.send( lbxapi: '1.1' );
        );

        app.listen(3050, function()
        console.log("Express running");
        );





        share|improve this answer













        Thanks to the comment by Ariel Alvarado (= using default error handler), I can answer my own question:



        var express = require("express");

        var app = express();
        app.use(express.json());

        // added this error handler
        app.use(function (err, req, res, next)
        console.error(err.stack)
        res.status(500).send('Something broke!')
        );

        app.post("/hackme", (req, res) =>

        if(!req.body.foo)
        return res.send( error: 'oh no' );


        return res.send( lbxapi: '1.1' );
        );

        app.listen(3050, function()
        console.log("Express running");
        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 19:21









        user3601578user3601578

        5021517




        5021517





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326179%2fhide-json-parse-errors-in-express-post%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線