Rendering server side errors on the client in Node/Express

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I've set up a simple Node/Express service that creates new bookmarks to a database.
In my Bookmark schema, one of the fields, URL, has validations. When you try to create a new bookmark without the correct validations, it throws an error and a message.
In other words, if you don't provide a URL to the request, then it will throw a message: "Please provide a URL."
The route for the POST request to create a new bookmark:
app.post(
'/api/bookmarks',
(req, res) =>
const url, tags = req.body;
const bookmark = new Bookmark(
url,
tags
);
bookmark.save()
.then(res =>
res.send(result)
)
.catch(error =>
console.log("err:", error);
res.status(400).send(error)
);
);
What my Bookmark schema looks like:
const bookmarkSchema = new Schema(
url:
type: String,
required: [true, "Please provide a URL."],
match: [urlRegex, "Your URL is invalid. Please make sure to add www."]
,
tags: [String]
);
When I make an API call on the front end client (e.g. via React), I am able to catch the error and subsequent message (Please provide a URL.), but as you can see, I have to call err.response.data.errors.url.message which seems very verbose and long winded.
axios.post('http://localhost:5000/api/bookmarks', tags: ["banana", "orange"] )
.then(result => console.log('Result:', result.data))
.catch(err => console.log('Error:', err.response.data.errors.url.message));
It has me thinking - is there a better way to render errors here? Would it be better handle errors in the client side instead?
I'm quite new to error handling so would like to know what is considered best practice.
javascript node.js error-handling express.js
add a comment |Â
up vote
1
down vote
favorite
I've set up a simple Node/Express service that creates new bookmarks to a database.
In my Bookmark schema, one of the fields, URL, has validations. When you try to create a new bookmark without the correct validations, it throws an error and a message.
In other words, if you don't provide a URL to the request, then it will throw a message: "Please provide a URL."
The route for the POST request to create a new bookmark:
app.post(
'/api/bookmarks',
(req, res) =>
const url, tags = req.body;
const bookmark = new Bookmark(
url,
tags
);
bookmark.save()
.then(res =>
res.send(result)
)
.catch(error =>
console.log("err:", error);
res.status(400).send(error)
);
);
What my Bookmark schema looks like:
const bookmarkSchema = new Schema(
url:
type: String,
required: [true, "Please provide a URL."],
match: [urlRegex, "Your URL is invalid. Please make sure to add www."]
,
tags: [String]
);
When I make an API call on the front end client (e.g. via React), I am able to catch the error and subsequent message (Please provide a URL.), but as you can see, I have to call err.response.data.errors.url.message which seems very verbose and long winded.
axios.post('http://localhost:5000/api/bookmarks', tags: ["banana", "orange"] )
.then(result => console.log('Result:', result.data))
.catch(err => console.log('Error:', err.response.data.errors.url.message));
It has me thinking - is there a better way to render errors here? Would it be better handle errors in the client side instead?
I'm quite new to error handling so would like to know what is considered best practice.
javascript node.js error-handling express.js
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've set up a simple Node/Express service that creates new bookmarks to a database.
In my Bookmark schema, one of the fields, URL, has validations. When you try to create a new bookmark without the correct validations, it throws an error and a message.
In other words, if you don't provide a URL to the request, then it will throw a message: "Please provide a URL."
The route for the POST request to create a new bookmark:
app.post(
'/api/bookmarks',
(req, res) =>
const url, tags = req.body;
const bookmark = new Bookmark(
url,
tags
);
bookmark.save()
.then(res =>
res.send(result)
)
.catch(error =>
console.log("err:", error);
res.status(400).send(error)
);
);
What my Bookmark schema looks like:
const bookmarkSchema = new Schema(
url:
type: String,
required: [true, "Please provide a URL."],
match: [urlRegex, "Your URL is invalid. Please make sure to add www."]
,
tags: [String]
);
When I make an API call on the front end client (e.g. via React), I am able to catch the error and subsequent message (Please provide a URL.), but as you can see, I have to call err.response.data.errors.url.message which seems very verbose and long winded.
axios.post('http://localhost:5000/api/bookmarks', tags: ["banana", "orange"] )
.then(result => console.log('Result:', result.data))
.catch(err => console.log('Error:', err.response.data.errors.url.message));
It has me thinking - is there a better way to render errors here? Would it be better handle errors in the client side instead?
I'm quite new to error handling so would like to know what is considered best practice.
javascript node.js error-handling express.js
I've set up a simple Node/Express service that creates new bookmarks to a database.
In my Bookmark schema, one of the fields, URL, has validations. When you try to create a new bookmark without the correct validations, it throws an error and a message.
In other words, if you don't provide a URL to the request, then it will throw a message: "Please provide a URL."
The route for the POST request to create a new bookmark:
app.post(
'/api/bookmarks',
(req, res) =>
const url, tags = req.body;
const bookmark = new Bookmark(
url,
tags
);
bookmark.save()
.then(res =>
res.send(result)
)
.catch(error =>
console.log("err:", error);
res.status(400).send(error)
);
);
What my Bookmark schema looks like:
const bookmarkSchema = new Schema(
url:
type: String,
required: [true, "Please provide a URL."],
match: [urlRegex, "Your URL is invalid. Please make sure to add www."]
,
tags: [String]
);
When I make an API call on the front end client (e.g. via React), I am able to catch the error and subsequent message (Please provide a URL.), but as you can see, I have to call err.response.data.errors.url.message which seems very verbose and long winded.
axios.post('http://localhost:5000/api/bookmarks', tags: ["banana", "orange"] )
.then(result => console.log('Result:', result.data))
.catch(err => console.log('Error:', err.response.data.errors.url.message));
It has me thinking - is there a better way to render errors here? Would it be better handle errors in the client side instead?
I'm quite new to error handling so would like to know what is considered best practice.
javascript node.js error-handling express.js
edited May 28 at 1:34
Jamalâ¦
30.1k11114225
30.1k11114225
asked May 3 at 9:24
doctopus
1062
1062
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193544%2frendering-server-side-errors-on-the-client-in-node-express%23new-answer', 'question_page');
);
Post as a guest
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
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
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