throw error if text field in balabola's response is empty

This commit is contained in:
Timofey Gelazoniya 2022-11-14 00:42:09 +03:00
parent 88afaba979
commit 5cef69e41b
Signed by: zeldon
GPG Key ID: 047886915281DD2A
2 changed files with 9 additions and 1 deletions

View File

@ -13,6 +13,7 @@ interface BalabolaResponse {
}
export class BalabolaError extends Error { }
export class BalabolaEmptyText extends Error { }
const BASE_URL = 'https://yandex.ru/lab/api/yalm/text3';
@ -29,6 +30,7 @@ export async function balabola(query: string, intro: number): Promise<string> {
}).then(res => res.json()) as BalabolaResponse;
if (result.bad_query === 1) throw new BalabolaError('bad query');
if (!result.text) throw new BalabolaEmptyText('empty text');
return result.text;
}

View File

@ -1,5 +1,5 @@
import { APIError, MessageContext } from "vk-io";
import { BalabolaError } from "../balabola_api";
import { BalabolaEmptyText, BalabolaError } from "../balabola_api";
import { Composer } from "../composer";
import { logger } from "../logger";
@ -29,6 +29,12 @@ composer.use(async (ctx, next) => {
return;
}
if (err instanceof BalabolaEmptyText) {
logger.error('empty text');
await ctx.reply('сорян тупанул не палучилось сгенерировать давай потом');
return;
}
throw err;
}
});