From f998fad4bf785e44bc647d53423d88445dac5b5f Mon Sep 17 00:00:00 2001 From: xzeldon Date: Thu, 20 Oct 2022 19:37:19 +0300 Subject: [PATCH] balabola error --- src/balabola_api.ts | 4 ++++ src/handlers/error.ts | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/balabola_api.ts b/src/balabola_api.ts index c1060b3..f6ea3ee 100644 --- a/src/balabola_api.ts +++ b/src/balabola_api.ts @@ -11,6 +11,8 @@ interface BalabolaResponse { signature: string; } +export class BalabolaError extends Error { } + const BASE_URL = 'https://yandex.ru/lab/api/yalm/text3'; export async function balabola(query: string, intro: number): Promise { @@ -23,5 +25,7 @@ export async function balabola(query: string, intro: number): Promise { }) }).then(res => res.json()) as BalabolaResponse; + if (result.bad_query === 1) throw new BalabolaError('bad query'); + return result.text; } \ No newline at end of file diff --git a/src/handlers/error.ts b/src/handlers/error.ts index 0dccc1e..95742f8 100644 --- a/src/handlers/error.ts +++ b/src/handlers/error.ts @@ -1,4 +1,5 @@ import { APIError, MessageContext } from "vk-io"; +import { BalabolaError } from "../balabola_api"; import { Composer } from "../composer"; import { logger } from "../logger"; @@ -22,6 +23,12 @@ composer.use(async (ctx, next) => { return; } + if (err instanceof BalabolaError) { + logger.error('bad query'); + await ctx.send('бабабола не хочет такое генерировать(('); + return; + } + throw err; } }); \ No newline at end of file