{"record":{"id":"242674634e9361c0","repo":"jackwener/OpenCLI","slug":"no-weread-books-found-for-bookquery","errorCode":null,"errorMessage":"No WeRead books found for \"${bookQuery}\"","messagePattern":"No WeRead books found for \"(.+?)\"","errorType":"exception","errorClass":"EmptyResultError","httpStatus":null,"severity":"warning","filePath":"clis/weread/book-search.js","lineNumber":225,"sourceCode":"    if (author) {\n        const exact = htmlEntries.filter((entry) => normalizeSearchText(entry.title) === title && normalizeSearchText(entry.author) === author);\n        if (exact.length === 1)\n            return exact[0].readerUrl;\n    }\n    const sameTitle = htmlEntries.filter((entry) => normalizeSearchText(entry.title) === title);\n    return sameTitle.length === 1 ? sameTitle[0].readerUrl : '';\n}\n\nasync function searchBookByQuery(bookQuery, bookRank) {\n    const url = new URL('/web/search/global', `${WEREAD_WEB_ORIGIN}/web`);\n    url.searchParams.set('keyword', bookQuery);\n    const data = await fetchJson(url, 'WeRead book search');\n    if (!Array.isArray(data?.books)) {\n        throw new CommandExecutionError('WeRead book search returned malformed books');\n    }\n    const books = data.books;\n    if (books.length === 0) {\n        throw new EmptyResultError('weread book-search', `No WeRead books found for \"${bookQuery}\"`);\n    }\n    if (bookRank > books.length) {\n        throw new ArgumentError(`book-rank must be <= ${books.length}`, `Only ${books.length} book search result(s) were returned for \"${bookQuery}\"`);\n    }\n    const bookInfo = books[bookRank - 1]?.bookInfo ?? {};\n    const selected = {\n        bookId: normalizeSearchText(bookInfo.bookId),\n        title: normalizeSearchText(bookInfo.title),\n        author: normalizeSearchText(bookInfo.author),\n        readerUrl: '',\n        chapters: [],\n    };\n    if (!selected.bookId) {\n        throw new CommandExecutionError(`WeRead book search result ${bookRank} is missing bookId`);\n    }\n    const htmlEntries = await loadSearchHtmlEntries(bookQuery);\n    selected.readerUrl = resolveReaderUrlForBook(selected, htmlEntries);\n    const readerMetadata = await loadReaderMetadata(selected.readerUrl);","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/weread/book-search.js#L207-L243","documentation":"searchBookByQuery throws EmptyResultError 'No WeRead books found for \"<query>\"' when the search API responds correctly but the books array is empty. This is a valid, expected outcome signaling that the query matched nothing — not a malfunction of the library.","triggerScenarios":"A book title/author/ISBN with zero matches in WeRead's catalog, heavy typos or overly specific multi-word queries, non-Chinese titles absent from WeRead, or region-restricted content invisible to the caller.","commonSituations":"Users paste full sentences as the query, search for obscure foreign books, include publisher/edition text that breaks matching, or query in a language WeRead does not index.","solutions":["Shorten the query to distinctive keywords (title + author)","Remove quotes/punctuation and extra words from the search string","Try alternate spellings or the original-language title","Verify the book actually exists on weread.qq.com via web search first","Handle EmptyResultError in scripts as a 'no match' branch, not a crash"],"exampleFix":"// before\nweread book-search --query \"The Three-Body Problem by Cixin Liu, translated by Ken Liu, first edition\"\n// after\nweread book-search --query \"三体 刘慈欣\"","handlingStrategy":"try-catch","validationCode":"// Preflight your query: reject empty/overly long free-text before calling the CLI\nconst q = rawQuery.trim().replace(/[\"'\\u201c\\u201d]/g, '');\nif (q.length < 2) throw new Error('Search query too short or empty');","typeGuard":"const isUsableQuery = (v) => typeof v === 'string' && v.trim().length >= 2;","tryCatchPattern":"try {\n  await runCommand(['book-search', '--query', q]);\n} catch (e) {\n  if (e instanceof EmptyResultError || e.message.includes('No WeRead books found')) {\n    console.log(`No results for \"${q}\" — try shorter keywords or the original title.`);\n    process.exitCode = 0; // treat as empty, not failure\n  } else throw e;\n}","preventionTips":["Search with 2-4 distinctive keywords (title and/or author) instead of full sentences","Verify the book exists on weread.qq.com before scripting against it","Strip quotes, punctuation, and edition/publisher noise from queries","Treat EmptyResultError as a normal no-match outcome in batch scripts and continue"],"tags":["empty-results","search","user-input"],"backgroundTag":"empty-search-results","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}