Skip to content

Commit

Permalink
add fallback error response in case ICU was not initialized before error
Browse files Browse the repository at this point in the history
  • Loading branch information
VladBrok committed Sep 30, 2023
1 parent c324686 commit de974e3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions api/helpers/handle-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { ReqBody, ResBody } from "alice-types";
import end from "./end-response";
import { VercelResponse } from "@vercel/node";
import requestAuth from "./request-auth";
import { t } from 'i18next';
import { t } from "i18next";
import { ERROR_RESPONSE } from "../../constants";

export default function handleError(
e: unknown,
res: VercelResponse,
body: ReqBody
) {
console.error(e); // TODO: report an error
console.error(e);

if ((e as any).httpStatusCode == 401) {
requestAuth(res, body);
Expand All @@ -19,11 +20,11 @@ export default function handleError(
const answer: ResBody = {
version: body.version,
response: {
text: t('handle_error'),
text: t("handle_error") || ERROR_RESPONSE, // fallback in case an error occurs before or during ICU initialization
end_session: false,
},
};
end(res, answer);
}

//
//
3 changes: 2 additions & 1 deletion api/helpers/icu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import i18next from "i18next";
import ICU from "i18next-icu";
import { ERROR_RESPONSE } from "../../constants";

const translation = {
greet_new_user: `Добро пожаловать!\nВ навыке Вы можете управлять своими задачами Todoist.\nСкажите "задачи", чтобы узнать список открытых задач.`,

handle_error: `Ой, я не смогла обработать запрос.\nПожалуйста, повторите его, или скажите "что ты умеешь" для просмотра доступных действий`,
handle_error: ERROR_RESPONSE,

auth_not_supported: `Извините, эта поверхность не поддерживает авторизацию.\nПопробуйте запустить навык с телефона`,

Expand Down
5 changes: 4 additions & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ async function handler(req: VercelRequest, res: VercelResponse) {

const body = req?.body as ReqBody;
try {
throw new Error("oops"); // TODO: remove
// TODO: remove
console.log("wow omg");
throw new Error("oops");

await initICU();

const supportsAuth = Boolean(body.meta.interfaces.account_linking);
Expand Down
1 change: 1 addition & 0 deletions constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const PAGE_SIZE = 10;
export const ERROR_RESPONSE = `Ой, я не смогла обработать запрос.\nПожалуйста, повторите его, или скажите "что ты умеешь" для просмотра доступных действий`;

0 comments on commit de974e3

Please sign in to comment.