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

TypeError: Cannot convert undefined or null to object #24

Open
honzajavorek opened this issue Aug 16, 2019 · 4 comments
Open

TypeError: Cannot convert undefined or null to object #24

honzajavorek opened this issue Aug 16, 2019 · 4 comments
Labels

Comments

@honzajavorek
Copy link
Contributor

honzajavorek commented Aug 16, 2019

Sometimes gavel2html fails to render the diff for some reason. We're able to see this when using gavel2html internally in apiary.io, but we don't know (yet) what's the exact input to trigger this.

gavelhtml: Internal error. 
 [TypeError: Cannot convert undefined or null to object 
  at Function.keys (<anonymous>:null:null) 
  at HeadersResultConverter._getLines (/app/node_modules/gavel2html/lib/headers-result-converter.js:27:37) 
  at HeadersResultConverter.getHtmlPrivate (/app/node_modules/gavel2html/lib/headers-result-converter.js:71:18) 
  at HeadersResultConverter.Converter.getHtml (/app/node_modules/gavel2html/lib/converter.js:46:17) 
  at Gavel2Html.getHtml (/app/node_modules/gavel2html/lib/gavel2html.js:61:26) 
  ...
]
@honzajavorek
Copy link
Contributor Author

It seems like if this line

for k, v of dataRealWithExpected then do (k, v) =>

blew up because dataRealWithExpected is not an object but undefined.

for k, v of dataRealWithExpected then do (k, v) =>
  console.log(k, v)

translates to

var k, v;

for (k in dataRealWithExpected) {
  v = dataRealWithExpected[k];
  ((k, v) => {
    return console.log(k, v);
  })(k, v);
}

Where I believe the for ... in statement might blow up with a similar error if it got to iterate over undefined (hunch, not verified).

@honzajavorek
Copy link
Contributor Author

Surprisingly, iterating over undefined like this doesn't trigger any error:

> const obj = undefined;
> for (k in obj) { console.log(k); }
undefined

Accessing a property throws a different errorr:

> obj['foo']
TypeError: Cannot read property 'foo' of undefined

But I got completely the same error if calling Object.keys() on undefined:

> Object.keys(obj)
TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)

That means the line numbers are probably off and the problem raises in these two lines:

lowercasedExpectedKeys = Object.keys(@dataExpected).map (s) -> s.toLowerCase()
lowercasedRealKeys = Object.keys(@dataReal).map (s) -> s.toLowerCase()

Both lines are a product of @getFromString:

@dataReal = @getFromString @dataReal
@dataExpected = @getFromString @dataExpected

There is a path in code which can result in the data not being an object:

if not header or typeof header isnt 'string'
return header

However, subsequent code doesn't seem to count with that option.

@honzajavorek
Copy link
Contributor Author

So the issue is that headers are set to undefined instead of {}. There are two steps to mitigation of this issue:

  • We should check what exactly we're sending to gavel2html on apiary.io side
  • gavel2html should be better in validating inputs and throwing when something's wrong so the code calling it can somehow react to this situation - instead of rendering HTML with Internal validator error and performing console.error()

@madhurab20
Copy link

convert undefined or null to object
at Function.keys ()
at DynamicListingComponent.push../src/app/shared/dynamic-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants