Skip to content

Error handling in setApiKey #1068

@rockert42

Description

@rockert42

Issue Summary

I'm retrieving the api key from an environment variable. Somehow in one of our environments the api key was undefined. It would be helpful if an error is logged or an error is thrown when setupApiKey is called with undefined, or with other wrong inputs, so that we are immediately informed when this is the case.

Steps to Reproduce

  1. Setup the api key as undefined sgMail.setupApiKey(undefined)
  2. No error, so it seems like the ApiKey is set correctly
  3. Only when trying to send a mail an error is thrown

Code Snippet

const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.setApiKey(undefined);
sgMail.send(msg);

Exception/Log

None after setupApiKey(undefined)

Error after sending the mail;

Unauthorized (401)
  Permission denied, wrong credentials

Technical details:

  • sendgrid-nodejs version: 6.5.4
  • node version: 8.16.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions