sendgrid-nodejs: Callback function signature does not follow Node conventions

This is probably a decision you made to do things a certain way, but it seems with the 3.0.4 version of this library, the callback passed to sg.API does not follow the standard Node callback function signature convention of function (error, data). Instead, the API response is always returned as the first object.

This makes it hard to determine if something went wrong, as we are now responsible for deciphering the raw API response and analyse the response headers and status codes manually.

I don’t feel that this should be the responsibility of user land, but of the library itself. What if status codes change? What if the response format changes? What if an error occurs and the returned data format becomes different, or null or undefined?

The very least that can be done to address this is for the library to determine if an error occurred, and if yes, populate the first parameter with an error object (it can have the response attached to it). If no error occurs, return null as the first parameter and the response object as the second parameter.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 5
  • Comments: 25 (24 by maintainers)

Most upvoted comments

My current solution is to check the status code for success, e.g.:

function sendMail(mail) {
  return new Promise((resolve, reject) => {

    //Build request
    let request = sg.emptyRequest();
    request.method = 'POST';
    request.path = '/v3/mail/send';
    request.body = mail.toJSON();

    //Send request
    sg.API(request, response => {
      if (response && response.statusCode &&
        response.statusCode >= 200 && response.statusCode <= 299) {
        resolve(response);
      }
      reject(new SendMailError(
        'Sendgrid response error ' + response.statusCode
      ));
    });
  });
}

As you can see this is quite verbose though and requires a lot of boilerplate to simply send an email and check if it worked or not.

Proposed API would be something like:

function sendMail(mail) {
  return new Promise((resolve, reject) => {

    //Build request
    let request = sg.emptyRequest();
    request.method = 'POST';
    request.path = '/v3/mail/send';
    request.body = mail.toJSON();

    //Send request
    sg.API(request, (error, response) => {
      if (error {
        reject(new SendMailError(error);
      }
      resolve(response);
    });
  });
}

Or better yet, with promises returned:

function sendMail(mail) {

  //Build request
  let request = sg.emptyRequest();
  request.method = 'POST';
  request.path = '/v3/mail/send';
  request.body = mail.toJSON();

  //Send request
  return sg.API(request);
}

And, ideally, we’d see the return of a dedicated mail sending helper which would create the proper request for us in the background, eliminating the need for a sendMail helper and boilerplate altogether:

sg.sendMail(mail);

If this is something you are interested in pursuing I will look into making some PR’s for it.

Agreed.

I like this option: “Check if promises are supported or not, throw an error if they aren’t but if people try to use them”, but also allowing users to specify the implementation they would like.

@thinkingserious I am also implementing a Promise API, but native promises were only introduced in Node 0.11.13.

Several solutions, let me know which (or combination) you prefer:

  • Use bluebird promises as a dependency
  • Check if promises are supported or not, throw an error if they aren’t but if people try to use them
  • Allow users to set the promise implementation they want to use by simply setting sendgrid.Promise to whatever value.

Probably a combination of 2 and 3 would be simplest and most flexible.

Ok, I’ve kept the var’s and just fixed code style issues that came up. Created a PR for that separately from this issue.

@adambuczynski,

I’m happy to use yours, it looks solid 😃

Please add it as part of your commit.

There is no .eslintrc.yaml config file in your project. Could you add/create one with your preferred rules so that I can use it as well?

Hi guys, haven’t forgotten about this, but I’ve been a bit busy with several other projects. Still hope to look into this at some stage 😃

@adambuczynski we have received it and thanks for the feedback!

@thinkingserious I’ve just mailed you the signed CA, I hope to have some time next week to look into this.

The suggestions @adambuczynski made are spot on. I just updated to the v3 API and was very disappointed that I can’t use a simple helper function anymore and instead have to write a bunch of boiler plate code. I can no longer even wrap it in a Bluebird promise without writing ugly error handling code.

Yes I have seen the helper for mail. Do you want to create a mail sending helper on that object?