Skip to content

Commit

Permalink
fix(Booru): fix rule34.xxx erroring if searching tags that give 0 res…
Browse files Browse the repository at this point in the history
…ults
  • Loading branch information
AtoraSuunva committed Jul 13, 2022
1 parent 8f22c27 commit 5a9dd85
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# booru Changelog

## 2.5.8

- Fix error when searching rule34.xxx with tags that return no results
- For some reason, the API literally returns an empty response with HTTP 200 instead of an empty array (or at least a different status code)

## 2.5.7

- Fix error when splitting by tags when konachan returned an empty string ([thanks youliao](https://github.com/AtoraSuunva/booru/pull/88))
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "booru",
"version": "2.5.7",
"version": "2.5.8",
"description": "Search (and do other things) on a bunch of different boorus!",
"author": "AtoraSuunva (https://github.com/AtoraSuunva/)",
"license": "MIT",
Expand Down
15 changes: 15 additions & 0 deletions src/Utils.ts
Expand Up @@ -94,6 +94,21 @@ export function jsonfy(xml: string): object[] {
return []
}

/**
* Try to parse JSON, and then return an empty array if data is an empty string, or the parsed JSON
*
* Blame rule34.xxx for returning literally an empty response with HTTP 200 for this
* @param data The data to try and parse
* @returns Either the parsed data, or an empty array
*/
export function tryParseJSON(data: string): Record<string, unknown>[] {
if (data === '') {
return []
}

return JSON.parse(data)
}

/**
* Yay fisher-bates
* Taken from http://stackoverflow.com/a/2450976
Expand Down
6 changes: 3 additions & 3 deletions src/boorus/Booru.ts
Expand Up @@ -5,7 +5,7 @@

import { fetch } from 'undici'
import { BooruError, defaultOptions, searchURI } from '../Constants'
import { jsonfy, resolveSite, shuffle } from '../Utils'
import { jsonfy, resolveSite, shuffle, tryParseJSON } from '../Utils'

import InternalSearchParameters from '../structures/InternalSearchParameters'
import Post from '../structures/Post'
Expand Down Expand Up @@ -193,8 +193,8 @@ export class Booru {
}
}

const data = xml ? await response.text() : await response.json()
const posts = xml ? jsonfy(data as string) : data
const data = await response.text()
const posts = xml ? jsonfy(data) : tryParseJSON(data)

if (!response.ok) {
throw new BooruError(
Expand Down

0 comments on commit 5a9dd85

Please sign in to comment.