l2studio / iqdb-api

🔍 A library for iqdb.org image search api
Apache License 2.0
11 stars 0 forks source link

TypeError if a possible result isn't in the format [Image Dimensions] [Type] #3

Closed GitThirteen closed 1 year ago

GitThirteen commented 1 year ago

Good morning/afternoon/evening,

I'd like to report what is likely a bug due to IQDB (or rather, probably Gelbooru) rarely returning an [Unrated] without any image dimensions specified, which in turn makes line 186 in api.js fail with a TypeError, as dimensionTexts[1] ends up being undefined.

function parseResultsPage($, $pageEl) {
  // ...
  const $dimensionEl = $($rows[$matchEl.length + 2]).find('td');
  const dimensionTexts = $dimensionEl.text().trim().split(' ');
  const dimension = dimensionTexts[0].replace(/[^\d]/g, '-').split('-');
  const width = parseInt(dimension[0]);
  const height = parseInt(dimension[1]);
  const type = dimensionTexts[1].substring(1, dimensionTexts[1].length - 1).toLowerCase(); // error occurs here
  const $similarityEl = $($rows[$matchEl.length + 3]).find('td');
  const similarityTexts = $similarityEl.text().trim().split(' ');
  // ...

Edit: I just cross-checked with the code in the repository, it corresponds to line 214 in api.ts.

// dimensionTexts[1] is undefined, therefore .substring() causes a TypeError
const type = dimensionTexts[1].substring(1, dimensionTexts[1].length - 1).toLowerCase() 

As seen here, the dimensionTexts aren't in the format WidthXHeight [Type], but just [Type]: error while the code assumes that the result should look like this: expected

Amazing library otherwise! Cheers.

lgou2w commented 1 year ago

Thank you very much for your feedback. This bug has been confirmed!

lgou2w commented 1 year ago

This bug was fixed in v0.2.1