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()
|
||||
);
|
||||
delete res.results_source;
|
||||
const searchResults = Object.values(res)
|
||||
.filter((result) => !("did_you_mean" in result))
|
||||
.splice(0, Number(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)}>`;
|
||||
if (res.error?.message) {
|
||||
if (res.error.message.indexOf("No results found.") > -1) {
|
||||
return "Search returned no results.";
|
||||
} else {
|
||||
out += `**${safeString(
|
||||
parseHtmlEntities(result.title)
|
||||
).trim()}** - <${encodeURI(result.url)}>`;
|
||||
out += `\n> ${safeString(parseHtmlEntities(result.description))}`;
|
||||
return `Search returned error:\n\`\`\`\n${res.error.message}\`\`\``;
|
||||
}
|
||||
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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue