sendinblue / APIv3-nodejs-library

SendinBlue's API v3 Node.js Library
ISC License
102 stars 47 forks source link

Error type returned not the good one #110

Closed leoguer closed 3 years ago

leoguer commented 3 years ago

Hi there, I got a problem with the error type returned. i used the code below :

apiInstance.createContact(createContact).then(function(data) { console.log('API called successfully. Returned data: ' + data); }, function(error) { console.log(error.code); });

But when i console log the error i got this :

Error: Unsuccessful HTTP response at S. (bundle.js?ver=1.08:10) at S.n.emit (bundle.js?ver=1.08:10) at XMLHttpRequest.e.onreadystatechange (bundle.js?ver=1.08:10)

In your documentation the error code may have a message and a code but here i can't find them.

do you have suggestion to find them ?

Best Regards,

GUERARD Léo

shubhamUpadhyayInBlue commented 3 years ago

Hi @leoguer

I tried to replicate the issue but could not. However, I tried to send an empty body using this script:

let SibApiV3Sdk = require('sib-api-v3-sdk');
let defaultClient = SibApiV3Sdk.ApiClient.instance;

let apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = 'YOUR_API_KEY';

let apiInstance = new SibApiV3Sdk.ContactsApi();

let createContact = new SibApiV3Sdk.CreateContact(); // CreateContact | Values to create a contact

apiInstance.createContact(createContact).then(function(data) {
  console.log('API called successfully. Returned data: ' + data);
}, function(error) {
  console.error(error);
});

The error object is printed something like this: Screenshot 2021-08-03 at 11 32 04 AM

So, the correct way to know where exactly in the object the message and code are present is to try to print the error object like I have done in the script above console.error(error);.

Another way is to print all the keys in an object is using Object.keys(error);.

Now if you want to print the message and status in error you have to do something like this: console.error(error.response.body);

Happy to help!