How can I access the data from new Error(data) in catch method?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Here is my promise:



.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});


The console.log that is executed just prior to throw Error(response); shows this nice information:



will throw error 
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}


But the console.log("catching error", error) in the catch method shows



catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)


Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:



catching error {}



  • How can I access the data I passed to the new error?

  • How should I fix this?










share|improve this question























  • Try logging the error.message intead

    – Bergi
    Nov 16 '18 at 15:23


















1















Here is my promise:



.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});


The console.log that is executed just prior to throw Error(response); shows this nice information:



will throw error 
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}


But the console.log("catching error", error) in the catch method shows



catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)


Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:



catching error {}



  • How can I access the data I passed to the new error?

  • How should I fix this?










share|improve this question























  • Try logging the error.message intead

    – Bergi
    Nov 16 '18 at 15:23














1












1








1








Here is my promise:



.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});


The console.log that is executed just prior to throw Error(response); shows this nice information:



will throw error 
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}


But the console.log("catching error", error) in the catch method shows



catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)


Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:



catching error {}



  • How can I access the data I passed to the new error?

  • How should I fix this?










share|improve this question














Here is my promise:



.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});


The console.log that is executed just prior to throw Error(response); shows this nice information:



will throw error 
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}


But the console.log("catching error", error) in the catch method shows



catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)


Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:



catching error {}



  • How can I access the data I passed to the new error?

  • How should I fix this?







javascript promise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 15:10









user1283776user1283776

3,7132160117




3,7132160117













  • Try logging the error.message intead

    – Bergi
    Nov 16 '18 at 15:23



















  • Try logging the error.message intead

    – Bergi
    Nov 16 '18 at 15:23

















Try logging the error.message intead

– Bergi
Nov 16 '18 at 15:23





Try logging the error.message intead

– Bergi
Nov 16 '18 at 15:23












2 Answers
2






active

oldest

votes


















1














The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:



throw new Error(JSON.stringify(response))


Then you can access that message in your catch handler with



error.message


or even



JSON.parse(error.message)


if you need to interact with the individual properties for some reason.






share|improve this answer

































    2














    I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.



    From docs:




    Displays an interactive list of the properties of the specified
    JavaScript object. The output is presented as a hierarchical listing
    with disclosure triangles that let you see the contents of child
    objects.



    In other words, console.dir is the way to see all the properties of a
    specified JavaScript object in console by which the developer can
    easily get the properties of the object.




    Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.






    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',
      autoActivateHeartbeat: false,
      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%2f53340496%2fhow-can-i-access-the-data-from-new-errordata-in-catch-method%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:



      throw new Error(JSON.stringify(response))


      Then you can access that message in your catch handler with



      error.message


      or even



      JSON.parse(error.message)


      if you need to interact with the individual properties for some reason.






      share|improve this answer






























        1














        The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:



        throw new Error(JSON.stringify(response))


        Then you can access that message in your catch handler with



        error.message


        or even



        JSON.parse(error.message)


        if you need to interact with the individual properties for some reason.






        share|improve this answer




























          1












          1








          1







          The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:



          throw new Error(JSON.stringify(response))


          Then you can access that message in your catch handler with



          error.message


          or even



          JSON.parse(error.message)


          if you need to interact with the individual properties for some reason.






          share|improve this answer















          The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:



          throw new Error(JSON.stringify(response))


          Then you can access that message in your catch handler with



          error.message


          or even



          JSON.parse(error.message)


          if you need to interact with the individual properties for some reason.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 17:02

























          answered Nov 16 '18 at 15:17









          Ben StewardBen Steward

          1,261516




          1,261516

























              2














              I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.



              From docs:




              Displays an interactive list of the properties of the specified
              JavaScript object. The output is presented as a hierarchical listing
              with disclosure triangles that let you see the contents of child
              objects.



              In other words, console.dir is the way to see all the properties of a
              specified JavaScript object in console by which the developer can
              easily get the properties of the object.




              Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.






              share|improve this answer






























                2














                I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.



                From docs:




                Displays an interactive list of the properties of the specified
                JavaScript object. The output is presented as a hierarchical listing
                with disclosure triangles that let you see the contents of child
                objects.



                In other words, console.dir is the way to see all the properties of a
                specified JavaScript object in console by which the developer can
                easily get the properties of the object.




                Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.






                share|improve this answer




























                  2












                  2








                  2







                  I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.



                  From docs:




                  Displays an interactive list of the properties of the specified
                  JavaScript object. The output is presented as a hierarchical listing
                  with disclosure triangles that let you see the contents of child
                  objects.



                  In other words, console.dir is the way to see all the properties of a
                  specified JavaScript object in console by which the developer can
                  easily get the properties of the object.




                  Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.






                  share|improve this answer















                  I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.



                  From docs:




                  Displays an interactive list of the properties of the specified
                  JavaScript object. The output is presented as a hierarchical listing
                  with disclosure triangles that let you see the contents of child
                  objects.



                  In other words, console.dir is the way to see all the properties of a
                  specified JavaScript object in console by which the developer can
                  easily get the properties of the object.




                  Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 15:47

























                  answered Nov 16 '18 at 15:14









                  Commercial SuicideCommercial Suicide

                  10k113354




                  10k113354






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340496%2fhow-can-i-access-the-data-from-new-errordata-in-catch-method%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