Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 710 Bytes

success-failure-errors.md

File metadata and controls

39 lines (32 loc) · 710 Bytes

Handling Success/Failure/Errors

The send and sendMultiple methods return a Promise, so you can handle success and capture errors:

sgMail
  .send(msg)
  .then(() => {
    // Celebrate
  })
  .catch(error => {
    // Log friendly error
    console.error(error);

    if (error.response) {
      // Extract error msg
      const {message, code, response} = error;

      // Extract response msg
      const {headers, body} = response;

      console.error(body);
    }
  });

Alternatively, pass a callback function as the last parameter:

sgMail
  .send(msg, (error, result) => {
    if (error) {
      // Do something with the error
    }
    else {
      // Celebrate
    }
  });