Rendering server side errors on the client in Node/Express

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question



























    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.







    share|improve this question























      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.







      share|improve this question













      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.









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 28 at 1:34









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked May 3 at 9:24









      doctopus

      1062




      1062

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods