misc.search: better error handling
This commit is contained in:
parent
92e803b9bb
commit
5c7abc26aa
1 changed files with 34 additions and 22 deletions
|
@ -574,31 +574,43 @@ search.callback = async function (msg, line, args, {results = 2}) {
|
||||||
(res) => res.json()
|
(res) => res.json()
|
||||||
);
|
);
|
||||||
delete res.results_source;
|
delete res.results_source;
|
||||||
const searchResults = Object.values(res)
|
if (res.error?.message) {
|
||||||
.filter((result) => !("did_you_mean" in result))
|
if (res.error.message.indexOf("No results found.") > -1) {
|
||||||
.splice(0, Number(results));
|
return "Search returned no results.";
|
||||||
|
|
||||||
let out = `__**Results for \`${safeString(query)}\`**__\n`;
|
|
||||||
for (const result of searchResults) {
|
|
||||||
if (result.special_response) {
|
|
||||||
out +=
|
|
||||||
"> " +
|
|
||||||
safeString(
|
|
||||||
parseHtmlEntities(
|
|
||||||
result.special_response.response.split("\n").join("\n> ")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
out += `\n<${encodeURI(result.special_response.source)}>`;
|
|
||||||
} else {
|
} else {
|
||||||
out += `**${safeString(
|
return `Search returned error:\n\`\`\`\n${res.error.message}\`\`\``;
|
||||||
parseHtmlEntities(result.title)
|
|
||||||
).trim()}** - <${encodeURI(result.url)}>`;
|
|
||||||
out += `\n> ${safeString(parseHtmlEntities(result.description))}`;
|
|
||||||
}
|
}
|
||||||
out += "\n\n";
|
} else {
|
||||||
}
|
const searchResults = Object.values(res)
|
||||||
|
.filter((result) => !("did_you_mean" in result))
|
||||||
|
.splice(0, Number(results));
|
||||||
|
|
||||||
return out.trim();
|
if (searchResults.length > 0) {
|
||||||
|
let out = `__**Results for \`${safeString(query)}\`**__\n`;
|
||||||
|
for (const result of searchResults) {
|
||||||
|
if (result.special_response) {
|
||||||
|
out +=
|
||||||
|
"> " +
|
||||||
|
safeString(
|
||||||
|
parseHtmlEntities(
|
||||||
|
result.special_response.response.split("\n").join("\n> ")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
out += `\n<${encodeURI(result.special_response.source)}>`;
|
||||||
|
} else {
|
||||||
|
out += `**${safeString(
|
||||||
|
parseHtmlEntities(result.title)
|
||||||
|
).trim()}** - <${encodeURI(result.url)}>`;
|
||||||
|
out += `\n> ${safeString(parseHtmlEntities(result.description))}`;
|
||||||
|
}
|
||||||
|
out += "\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.trim();
|
||||||
|
} else {
|
||||||
|
return "Search returned no results.";
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
hf.registerCommand(search);
|
hf.registerCommand(search);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue