title: How to catch/return error in AJAX from server side nodejs when manually throwing error inside try block?
body: <p>Below is the server side code where I am manually throwing an error. It get caught inside catch block. But Now I want to send tat caught error back to the AJAX error field and throw an alert on UI.
  What I am doing wrong?</p>
  <pre><code>$.ajax({ 
    type: &quot;GET&quot;,
    url: &quot;/update-sources&quot;,
    dataType: &quot;json&quot;,
    data: {
      selectedValue: text,
      commonIDs: id,
      Id4AddtasksBigpaths:Id4AddtasksBigpaths
    },
    contentType: &quot;application/json&quot;,
    success: function(response){
      if (response.status == '200') {
  
        $.LoadingOverlay(&quot;hide&quot;);
  
        swal({
        title: &quot;In Progress&quot;,
        text: &quot;Status set to 'In Progress'&quot;,
        icon: &quot;success&quot;,
        button: &quot;Ok&quot;,
        showCancelButton: true
      }).then((result) =&gt; {
        if (result) {
            window.location.reload();
        }
      });
  
        }
      },
      error: function (request, ajaxOptions, thrownError) {
        $.LoadingOverlay(&quot;hide&quot;);
        alert(request.status);
        alert(thrownError);
    }
  });
  </code></pre>
  <p>Server side code:</p>
  <pre><code>router.route('/update-sources').get(async (req, res) =&gt; {
      try {
        var eleTopic, eleStatus;
        let data = await User.findOne({tag:&quot;Client&quot;,&quot;Addtasks.commonID&quot;:req.query.commonIDs});
  
        throw new Error(&quot;Just a try error!&quot;);
  
        let success = await User.updateMany({&quot;Addtasks.commonID&quot;:req.query.commonIDs},
        {$set: {&quot;Addtasks.$.width&quot; :'250px',
        &quot;Addtasks.$.height&quot; :'32px',
        &quot;Addtasks.$.background&quot; :'linear-gradient(45deg, #091B6A, #0029FF)',
        &quot;Addtasks.$.border_radius&quot; :'10px / 5px',
        &quot;Addtasks.$.status&quot; :req.query.selectedValue}});
        if (success) {
          console.log(&quot;In Progress color set!&quot;);
          let dataWriter = await User.findOne({tag:&quot;Writer&quot;,&quot;Addtasks.commonID&quot;:req.query.commonIDs})
          dataWriter.Addtasks.forEach(async (element) =&gt; {
            if(element.commonID == req.query.commonIDs)
            {
              await sendMails2User(dataWriter.email, element.topic, 'Requirement Submitted', 'In Progress');
              let dataAdmin = await User.findOne({tag:&quot;Admin&quot;,&quot;Addtasks.commonID&quot;:req.query.commonIDs})
              await sendMails2User(dataAdmin.email, element.topic, 'Requirement Submitted', 'In Progress');
              return res.end('{&quot;success&quot; : &quot;Updated Successfully&quot;, &quot;status&quot; : 200}');
            }
          })
        }
  
      } catch (err) {
        console.log(err)
      return res.end('{&quot;status&quot; : 400}');
      }
  
  })
  </code></pre>
  <p><strong>P.S.</strong> Currently the error is somehow not getting returned back to the AJAX error block and the overlay is keep displaying. (Not ending)</p>