sendwithus / sendwithus_nodejs

Sendwithus NodeJS Client
https://www.sendwithus.com
Apache License 2.0
22 stars 17 forks source link

send fails with "Response undefined: undefined" #53

Closed zeevl closed 2 years ago

zeevl commented 3 years ago

calling Sendwithus.send is failing every time -- had to downgrade to 4.3.1

Client version

4.3.3

Expected behaviour

email to send

Actual behaviour

Aug 13 12:07:18 lambda  2021-08-13T19:07:16.568Z    29cbee03-d790-4c1d-a976-35d61ef33039    INFO    SENDWITHUS: Built url: https://api.sendwithus.com/api/v1/send 
Aug 13 12:07:18 lambda   2021-08-13T19:07:16.568Z   29cbee03-d790-4c1d-a976-35d61ef33039    INFO    SENDWITHUS: Set headers: {"X-SWU-API-KEY":"----","X-SWU-API-CLIENT":"node-4.3.3"} 
Aug 13 12:07:18 lambda  2021-08-13T19:07:16.902Z    29cbee03-d790-4c1d-a976-35d61ef33039    INFO    SENDWITHUS: Response undefined: undefined 
Aug 13 12:07:18 lambda  2021-08-13T19:07:16.904Z api:apiHandler Error: Request failed with undefined 
Aug 13 12:07:18 lambda  at Sendwithus._handleResponse (/var/task/node_modules/sendwithus/lib/sendwithus.js:110:15) 
Aug 13 12:07:18 lambda  at /var/task/node_modules/sendwithus/lib/sendwithus.js:148:28 
Aug 13 12:07:18 lambda  at processTicksAndRejections (internal/process/task_queues.js:97:5) 
Aug 13 12:07:18 lambda   2021-08-13T19:07:17.180Z   29cbee03-d790-4c1d-a976-35d61ef33039    ERROR   Invoke Error    {"message":"Request failed with undefined","stack":"Error: Request failed with undefined\n    at Sendwithus._handleResponse (/var/task/node_modules/sendwithus/lib/sendwithus.js:110:15)\n    at /var/task/node_modules/sendwithus/lib/sendwithus.js:148:28\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)","cause":{"message":"Request failed with undefined","stack":"Error: Request failed with undefined\n    at Sendwithus._handleResponse (/var/task/node_modules/sendwithus/lib/sendwithus.js:110:15)\n    at /var/task/node_modules/sendwithus/lib/sendwithus.js:148:28\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)"},"isOperational":true} 

Steps to reproduce

call Sendwithus.send()

demoore commented 3 years ago

Thanks, @zeevl . To make this easier to reproduce can you let me know what version of node you're running and include an example of the send call you're making?

Thanks!

zeevl commented 3 years ago

Glad you asked, I suspect this might be part of the problem! We're promisifying Sendwithus so we can use it with async/await..

import SendWithUs from 'sendwithus';
import bluebird from 'bluebird';

const sendwithus = bluebird.promisifyAll(SendWithUs(process.env.SEND_WITH_US_KEY));

  await sendwithus.sendAsync({
    email_id: emailId,
    recipient,
    sender,
    email_data: data,
  });

And this is running in node 12.x (whatever their current version is) in an AWS lambda function...

kinano commented 2 years ago

I had the same issue when I updated from 4.3.1 to 4.3.3 to fix a build pipeline that broke when github turned off the unencrypted git protocol on November 2nd. I did not expect a patch version for this library to change a public API (I should have been more careful and skeptical).

I suggest adding a blurb on your readme to alert developers of this. Github will have a few more brownouts in the next few months and will remove support for unencrypted git protocol completely next March. I think it will be useful to help your community upgrade to a safe version that removes the unencrypted git protocol without breaking/changing the public stable API exposed in version 4.3.1.

https://github.blog/2021-09-01-improving-git-protocol-security-github/#when-are-these-changes-effective

The issue is caused by the reslter git:// url in package-lock.json generated by sendwithus 4.3.1. github shows the supported formats on their post.

"sendwithus": {
      "version": "4.3.1",
      "resolved": "https://registry.npmjs.org/sendwithus/-/sendwithus-4.3.1.tgz",
        ....
      "requires": {
        "restler": "git://github.com/sendwithus/restler.git#3.1.1"
      },
...
}
iprignano commented 2 years ago

We also experienced what is reported here. The issue was introduced after restler got replaced by node-fetch. I've opened a PR to address this: https://github.com/sendwithus/sendwithus_nodejs/pull/55

officert commented 2 years ago

ended up just writing my own SendWithUs client in all of about 5 mins. Here's the code if it's helpful for anyone else (uses Axios for HTTP client):

import axios from 'axios'
import SendWithUsApiError from '../errors/apiError'
import { SendWithUsSendEmailArgs } from '../types'

const BASE_URL = 'https://api.sendwithus.com/api/v1'

function handleError(error: any): SendWithUsApiError {
  let message = ''

  console.log('SENDWITHUS ERROR', error)

  if (error.response && error.response.data) {
    if (error.response.data.error) {
      message = error.response.data.error
    } else if (error.response.data.errors && error.response.data.errors.length) {
      message = JSON.stringify(error.response.data.errors)
    }
  }

  return new SendWithUsApiError(message)
}

const axiosInstance = axios.create({
  baseURL: BASE_URL,
})

class SendWithUsApiClient {
  async sendEmail(emailData: SendWithUsSendEmailArgs) {
    let result

    const apiKey = emailData.apiKey

    try {
      result = await axiosInstance({
        method: 'post',
        url: '/send',
        headers: {
          accept: 'application/json',
        },
        auth: {
          username: apiKey,
          password: '',
        },
        data: emailData,
      })
    } catch (error) {
      throw handleError(error)
    }

    return result.data
  }
}

export default new SendWithUsApiClient()

types:

export type SendwithUsFile = {
  id: string
  data: Buffer
}

export type SendWithUsSendEmailArgs = {
  apiKey: string
  template: string
  template_data?: object
  recipient: {
    address: string
    name?: string
  }
  sender: {
    address: string
    name?: string
    reply_to?: string
  }
  files?: Array<SendwithUsFile>
}