Hide JSON Parse Errors in Express POST
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> at JSON.parse (<anonymous>)
<br> at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> at IncomingMessage.emit (events.js:182:13)
<br> at endReadableNT (_stream_readable.js:1091:14)
<br> 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
add a comment |
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> at JSON.parse (<anonymous>)
<br> at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> at IncomingMessage.emit (events.js:182:13)
<br> at endReadableNT (_stream_readable.js:1091:14)
<br> 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
Show us the part of your code where you usedJSON.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 theprocess.env
variable?
– Samuel
Nov 15 '18 at 19:11
add a comment |
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> at JSON.parse (<anonymous>)
<br> at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> at IncomingMessage.emit (events.js:182:13)
<br> at endReadableNT (_stream_readable.js:1091:14)
<br> 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
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> at JSON.parse (<anonymous>)
<br> at parse (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/types/json.js:89:19)
<br> at /home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/body-parser/lib/read.js:121:18
<br> at invokeCallback (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:224:16)
<br> at done (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:213:7)
<br> at IncomingMessage.onEnd (/home/jan/Desktop/linkbox_beta2_github/code/experiments/node_modules/raw-body/index.js:273:7)
<br> at IncomingMessage.emit (events.js:182:13)
<br> at endReadableNT (_stream_readable.js:1091:14)
<br> 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
javascript node.js json express
asked Nov 15 '18 at 18:54
user3601578user3601578
5021517
5021517
Show us the part of your code where you usedJSON.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 theprocess.env
variable?
– Samuel
Nov 15 '18 at 19:11
add a comment |
Show us the part of your code where you usedJSON.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 theprocess.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
add a comment |
1 Answer
1
active
oldest
votes
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");
);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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");
);
add a comment |
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");
);
add a comment |
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");
);
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");
);
answered Nov 15 '18 at 19:21
user3601578user3601578
5021517
5021517
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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