Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle undefined multipart form value #212

Merged
merged 1 commit into from
Jan 30, 2019

Conversation

fijimunkii
Copy link
Contributor

Undefined values passed into lob.letters.create causes a confusing
exception in the form-data module that request uses for multipart headers

For instance, if the color key is set with no value, we get back this
confusing error: Error: Cannot read property 'name' of undefined

Now we get Error: color is undefined

@pallavi pallavi self-assigned this Jan 24, 2019
@pallavi
Copy link
Contributor

pallavi commented Jan 24, 2019

Hi @fijimunkii, I'm unable to reproduce the error you were seeing with undefined values.

Here's the code snippet I ran to test it. A letter was successfully created:

const Lob = require('lob')(process.env.API_KEY, { apiVersion: '2018-06-05' });

const config = {
  double_sided: true
};

Lob.letters.create({
  description: 'Lob-Node Undefined test',
  to: ADDRESS ID,
  from: ADDRESS ID,
  file: '<html style="padding-top: 3in; margin: .5in;">HTML Letter for {{name}}</html>',
  merge_variables: {
    name: 'Lob'
  },
  color: false,
  double_sided: config.double_sided,
  extra_service: config.extra_service  // undefined
})
.then((res) => console.log(res))
.catch((ex) => console.log(ex.message));

Which version of this library are you using? Thanks!

@fijimunkii
Copy link
Contributor Author

fijimunkii commented Jan 25, 2019

Hi @pallavi - Thanks your example works for me too!

So, it appears to be an issue with request and form-data. I've submitted PRs for it:
form-data/form-data#417
request/request#3097

In order to trigger this confusing error, the file has to be a stream (this way the lob package uses formData). Happens on node 10.10 and 11.3 with the latest version of this library

Here is code that throws Cannot read property 'name' of undefined

template.html

<h1>Hi {{name}}!</h1>

repro.js

const Lob = require('lob')(process.env.API_KEY, { apiVersion: '2018-06-05' });
const Fs   = require('fs');
const Path = require('path');

const config = {
  double_sided: true
};

Lob.letters.create({
  description: 'Lob-Node Undefined test',
  to: /* ADDRESS ID */,
  from: /* ADDRESS ID */,
  file: Fs.createReadStream(Path.join(__dirname,'./template.html')),
  merge_variables: {
    name: 'Lob'
  },
  color: false,
  double_sided: config.double_sided,
  extra_service: config.extra_service  // undefined
})
.then(console.log, console.log);

Copy link
Contributor

@pallavi pallavi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fijimunkii Thanks for the example, I was able to reproduce. Instead of throwing an error on an undefined value, we'd prefer to handle it silently and have a successful API request.
Also, could you add a unit test for this?

Thanks!

lib/resources/resourceBase.js Outdated Show resolved Hide resolved
Copy link
Contributor

@pallavi pallavi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for contributing :) We will merge your PR after one more approval.

  • @fijimunkii could you please squash your commits into a single commit that fits our commit message conventions: fix(resource-base): handle undefined multipart form value

  • @ the next reviewer: i confirmed that the build passes by importing the forked branch into our repo (https://travis-ci.org/lob/lob-node/builds/486536074) (the Travis build uses an encrypted environment variable, which is not accessible to pull requests from other repositories)

@pallavi pallavi assigned jesscxu, sjl2 and virtualdom and unassigned pallavi Jan 30, 2019
Copy link

@jesscxu jesscxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fijimunkii fijimunkii changed the title Throw error for undefined form value handle undefined multipart form value Jan 30, 2019
@pallavi pallavi merged commit 711261d into lob:master Jan 30, 2019
@fijimunkii fijimunkii deleted the error-undefined-form-value branch January 30, 2019 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

5 participants