apiaryio / gavel2html

Renders HTML diff from Gavel.js output
MIT License
0 stars 0 forks source link

TypeError: Cannot convert undefined or null to object #24

Open honzajavorek opened 5 years ago

honzajavorek commented 5 years ago

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 commented 5 years ago

It seems like if this line

https://github.com/apiaryio/gavel2html/blob/99897d716d44c85997399f6bb4ef8c9450cfbf26/src/headers-result-converter.coffee#L27

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 commented 5 years ago

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:

https://github.com/apiaryio/gavel2html/blob/99897d716d44c85997399f6bb4ef8c9450cfbf26/src/headers-result-converter.coffee#L16-L17

Both lines are a product of @getFromString:

https://github.com/apiaryio/gavel2html/blob/99897d716d44c85997399f6bb4ef8c9450cfbf26/src/headers-result-converter.coffee#L13-L14

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

https://github.com/apiaryio/gavel2html/blob/99897d716d44c85997399f6bb4ef8c9450cfbf26/src/headers-result-converter.coffee#L66-L67

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

honzajavorek commented 5 years ago

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

madhurab20 commented 2 years ago

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