throw error if text field in balabola's response is empty
This commit is contained in:
parent
88afaba979
commit
5cef69e41b
|
@ -13,6 +13,7 @@ interface BalabolaResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class BalabolaError extends Error { }
|
export class BalabolaError extends Error { }
|
||||||
|
export class BalabolaEmptyText extends Error { }
|
||||||
|
|
||||||
const BASE_URL = 'https://yandex.ru/lab/api/yalm/text3';
|
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;
|
}).then(res => res.json()) as BalabolaResponse;
|
||||||
|
|
||||||
if (result.bad_query === 1) throw new BalabolaError('bad query');
|
if (result.bad_query === 1) throw new BalabolaError('bad query');
|
||||||
|
if (!result.text) throw new BalabolaEmptyText('empty text');
|
||||||
|
|
||||||
return result.text;
|
return result.text;
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import { APIError, MessageContext } from "vk-io";
|
import { APIError, MessageContext } from "vk-io";
|
||||||
import { BalabolaError } from "../balabola_api";
|
import { BalabolaEmptyText, BalabolaError } from "../balabola_api";
|
||||||
import { Composer } from "../composer";
|
import { Composer } from "../composer";
|
||||||
import { logger } from "../logger";
|
import { logger } from "../logger";
|
||||||
|
|
||||||
|
@ -29,6 +29,12 @@ composer.use(async (ctx, next) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (err instanceof BalabolaEmptyText) {
|
||||||
|
logger.error('empty text');
|
||||||
|
await ctx.reply('сорян тупанул не палучилось сгенерировать давай потом');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
});
|
});
|
Loading…
Reference in New Issue