misc.search: better error handling

This commit is contained in:
Cynthia Foxwell 2024-05-04 12:58:55 -06:00
parent 92e803b9bb
commit 5c7abc26aa

View file

@ -574,10 +574,18 @@ search.callback = async function (msg, line, args, {results = 2}) {
(res) => res.json() (res) => res.json()
); );
delete res.results_source; delete res.results_source;
if (res.error?.message) {
if (res.error.message.indexOf("No results found.") > -1) {
return "Search returned no results.";
} else {
return `Search returned error:\n\`\`\`\n${res.error.message}\`\`\``;
}
} else {
const searchResults = Object.values(res) const searchResults = Object.values(res)
.filter((result) => !("did_you_mean" in result)) .filter((result) => !("did_you_mean" in result))
.splice(0, Number(results)); .splice(0, Number(results));
if (searchResults.length > 0) {
let out = `__**Results for \`${safeString(query)}\`**__\n`; let out = `__**Results for \`${safeString(query)}\`**__\n`;
for (const result of searchResults) { for (const result of searchResults) {
if (result.special_response) { if (result.special_response) {
@ -599,6 +607,10 @@ search.callback = async function (msg, line, args, {results = 2}) {
} }
return out.trim(); return out.trim();
} else {
return "Search returned no results.";
}
}
}; };
hf.registerCommand(search); hf.registerCommand(search);