How should my node/express RESTful API should return database errors to the clients?
up vote
0
down vote
favorite
Right now if there is an error, such as duplicate entry, I return it like this res.status(500).send(err);
Showing the client this kind of things:
"code": "ER_DUP_ENTRY",
"errno": 1062,
"sqlMessage": "Duplicate entry 'name@mail.com' for key 'user_table_email_unique'",
"sqlState": "23000",
"index": 0,
"sql": "update `user_table` set `email` = 'name@mail.com' where `id` = 3"
What is the standard way of doing this? I want the client to know what kind of error is but not, for example, the name of my tables
node.js express
add a comment |
up vote
0
down vote
favorite
Right now if there is an error, such as duplicate entry, I return it like this res.status(500).send(err);
Showing the client this kind of things:
"code": "ER_DUP_ENTRY",
"errno": 1062,
"sqlMessage": "Duplicate entry 'name@mail.com' for key 'user_table_email_unique'",
"sqlState": "23000",
"index": 0,
"sql": "update `user_table` set `email` = 'name@mail.com' where `id` = 3"
What is the standard way of doing this? I want the client to know what kind of error is but not, for example, the name of my tables
node.js express
Possibly opinion based?
– Electrox Mortem
Nov 9 at 19:50
1
It depends, is your client a normal user or a develop. In both cases, you should hide the table and field names and just send a custom error message. You can also just send http 409 to tell its a duplicate entry.
– Shaharyar
Nov 9 at 19:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Right now if there is an error, such as duplicate entry, I return it like this res.status(500).send(err);
Showing the client this kind of things:
"code": "ER_DUP_ENTRY",
"errno": 1062,
"sqlMessage": "Duplicate entry 'name@mail.com' for key 'user_table_email_unique'",
"sqlState": "23000",
"index": 0,
"sql": "update `user_table` set `email` = 'name@mail.com' where `id` = 3"
What is the standard way of doing this? I want the client to know what kind of error is but not, for example, the name of my tables
node.js express
Right now if there is an error, such as duplicate entry, I return it like this res.status(500).send(err);
Showing the client this kind of things:
"code": "ER_DUP_ENTRY",
"errno": 1062,
"sqlMessage": "Duplicate entry 'name@mail.com' for key 'user_table_email_unique'",
"sqlState": "23000",
"index": 0,
"sql": "update `user_table` set `email` = 'name@mail.com' where `id` = 3"
What is the standard way of doing this? I want the client to know what kind of error is but not, for example, the name of my tables
node.js express
node.js express
asked Nov 9 at 19:30
daniel gon
265
265
Possibly opinion based?
– Electrox Mortem
Nov 9 at 19:50
1
It depends, is your client a normal user or a develop. In both cases, you should hide the table and field names and just send a custom error message. You can also just send http 409 to tell its a duplicate entry.
– Shaharyar
Nov 9 at 19:52
add a comment |
Possibly opinion based?
– Electrox Mortem
Nov 9 at 19:50
1
It depends, is your client a normal user or a develop. In both cases, you should hide the table and field names and just send a custom error message. You can also just send http 409 to tell its a duplicate entry.
– Shaharyar
Nov 9 at 19:52
Possibly opinion based?
– Electrox Mortem
Nov 9 at 19:50
Possibly opinion based?
– Electrox Mortem
Nov 9 at 19:50
1
1
It depends, is your client a normal user or a develop. In both cases, you should hide the table and field names and just send a custom error message. You can also just send http 409 to tell its a duplicate entry.
– Shaharyar
Nov 9 at 19:52
It depends, is your client a normal user or a develop. In both cases, you should hide the table and field names and just send a custom error message. You can also just send http 409 to tell its a duplicate entry.
– Shaharyar
Nov 9 at 19:52
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
without seeing your code structure it's difficult to help, but if you have your query in a function that returns to your main script you could catch there, process the error and then throw a succinct and non-data exposing error back to the calling function.
// This is pseudocode
const updateEmail = (db, email, id) => db.query('query...')
.then(data => /* process things */)
.catch(err =>
if (err.errno === 1062)
throw new Error('Email already exists');
);
I'm not certain that 500 is the correct code to use in this particular case either, as it isn't a server error per se. That would be a preference thing though depending upon how your other requests are structured.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
without seeing your code structure it's difficult to help, but if you have your query in a function that returns to your main script you could catch there, process the error and then throw a succinct and non-data exposing error back to the calling function.
// This is pseudocode
const updateEmail = (db, email, id) => db.query('query...')
.then(data => /* process things */)
.catch(err =>
if (err.errno === 1062)
throw new Error('Email already exists');
);
I'm not certain that 500 is the correct code to use in this particular case either, as it isn't a server error per se. That would be a preference thing though depending upon how your other requests are structured.
add a comment |
up vote
0
down vote
without seeing your code structure it's difficult to help, but if you have your query in a function that returns to your main script you could catch there, process the error and then throw a succinct and non-data exposing error back to the calling function.
// This is pseudocode
const updateEmail = (db, email, id) => db.query('query...')
.then(data => /* process things */)
.catch(err =>
if (err.errno === 1062)
throw new Error('Email already exists');
);
I'm not certain that 500 is the correct code to use in this particular case either, as it isn't a server error per se. That would be a preference thing though depending upon how your other requests are structured.
add a comment |
up vote
0
down vote
up vote
0
down vote
without seeing your code structure it's difficult to help, but if you have your query in a function that returns to your main script you could catch there, process the error and then throw a succinct and non-data exposing error back to the calling function.
// This is pseudocode
const updateEmail = (db, email, id) => db.query('query...')
.then(data => /* process things */)
.catch(err =>
if (err.errno === 1062)
throw new Error('Email already exists');
);
I'm not certain that 500 is the correct code to use in this particular case either, as it isn't a server error per se. That would be a preference thing though depending upon how your other requests are structured.
without seeing your code structure it's difficult to help, but if you have your query in a function that returns to your main script you could catch there, process the error and then throw a succinct and non-data exposing error back to the calling function.
// This is pseudocode
const updateEmail = (db, email, id) => db.query('query...')
.then(data => /* process things */)
.catch(err =>
if (err.errno === 1062)
throw new Error('Email already exists');
);
I'm not certain that 500 is the correct code to use in this particular case either, as it isn't a server error per se. That would be a preference thing though depending upon how your other requests are structured.
answered Nov 9 at 19:54
D Lowther
1,2431414
1,2431414
add a comment |
add a comment |
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%2f53232189%2fhow-should-my-node-express-restful-api-should-return-database-errors-to-the-clie%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
Possibly opinion based?
– Electrox Mortem
Nov 9 at 19:50
1
It depends, is your client a normal user or a develop. In both cases, you should hide the table and field names and just send a custom error message. You can also just send http 409 to tell its a duplicate entry.
– Shaharyar
Nov 9 at 19:52