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










share|improve this question





















  • 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














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










share|improve this question





















  • 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












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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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.






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',
    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%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

























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 19:54









        D Lowther

        1,2431414




        1,2431414



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo