sendinblue / APIv3-nodejs-library

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

[BUG] API doesn't return explicit error messages. #62

Closed Marcosaurios closed 3 years ago

Marcosaurios commented 5 years ago

I've been implementing creating a new Contact on my Node API and I've found some strange kind of bug with SMS.

When I create a new contact with SMS<6 characters length returns this error message:

{
    "error": "Error: Bad Request\n    at Request.callback ...node_modules\\superagent\\lib\\node\\index.js:688:13)\n    at ...node_modules\\superagent\\lib\\node\\index.js:891:18\n    at IncomingMessage.<anonymous> ...node_modules\\superagent\\lib\\node\\parsers\\json.js:17:7)\n    at IncomingMessage.emit (events.js:194:15)\n    at endReadableNT (_stream_readable.js:1125:12)\n    at process._tickCallback (internal/process/next_tick.js:63:19)"
} 

But when I create a new one with SMS>6 characters length it creates ok. It seems to be in your superagent dependency. Or if you're validating SMS length (that I didn't find it in the repo) it will be better if the API error message will be something like 'Invalid SMS length' or so.

P.D.: I'm creating a new contact with that:

const SibApiV3Sdk = require('sib-api-v3-sdk');
const sendinblueClient = SibApiV3Sdk.ApiClient.instance;
const apiKey = sendinblueClient.authentications['api-key'];
apiKey.apiKey = process.env.SENDINBLUE_API_KEY;

const apiInstance = new SibApiV3Sdk.ContactsApi();
try {
  let createContact = new SibApiV3Sdk.CreateContact();

  createContact['email'] = p_body.email;
  createContact['attributes'] = {
    firstname: p_body.name,
    sms: p_body.sms
  };

  await apiInstance.createContact(createContact);
  return resolve(okmsg)
}
catch(err) {
  return reject(err);
}
shubhamUpadhyayInBlue commented 3 years ago

Hey @Marcosaurios

In order to print the exact error message as you would receive on hitting the APIv3 directly you need to print the error object as in the following code:

Also, you can open a ticket with our support team from your Sendinblue account so that they can get back to you promptly.

Note: You can print the other keys inside the error object using console.log(Object.keys(error)). Let me know if you need any help further.


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.email = 'example@example.com';
createContact.listIds = [2];
createContact.attributes = {
        "sms":"1234"
}

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