matthewmueller / x-ray

The next web scraper. See through the <html> noise.
MIT License
5.87k stars 349 forks source link

silently fails if img@src image element doesn't exist #249

Closed simonseddon closed 5 years ago

simonseddon commented 7 years ago

Could use either an error or better still a fallback value? I've tried adding an allow_undefined filter to check for undefined, null and false returns but it doesn't work so I expect the error happens a level too deep to intercept?

simonseddon commented 7 years ago

I used a filter to get around this. Originally I'd lost hope in a filter, but supplying value with a default of false made it possible. My filter is below and includes lots of negative checks, but I dare say you can get away with simply: value === false. Returning an empty string removes the value entirely from the final returned JSON.

var x = Xray({
  filters:({
      allow_undefined: function(value=false){
        if(
          !value.length ||
          value === null ||
          value === undefined ||
          value === false
        ){
          return ''
        }else{
          return value
        }
      }
  })
})
simonseddon commented 7 years ago

For brevity:

allow_undefined: function(value=false){
  return (value) ? value : ''
}
lathropd commented 5 years ago

Since this is solvable, I'm closing. Can reopen if someone is interested in tackling.