Skip to content

Commit

Permalink
Report errors to a dedicated telegram chat
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Oct 16, 2023
1 parent f4c864a commit ee060a3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
BOT_TOKEN=
SUPABASE_KEY=
SUPABASE_URL=
BOT_ERROR_REPORTING_TOKEN=
BOT_ERROR_REPORTING_USER_ID=



2 changes: 2 additions & 0 deletions functions/env/env-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const envSchema = z.object({
BOT_TOKEN: z.string(),
SUPABASE_KEY: z.string(),
SUPABASE_URL: z.string(),
BOT_ERROR_REPORTING_TOKEN: z.string().optional(),
BOT_ERROR_REPORTING_USER_ID: z.string().optional(),
});

export type EnvType = z.infer<typeof envSchema>;
5 changes: 3 additions & 2 deletions functions/health.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createJsonResponse } from "./lib/json-response/create-json-response.ts";
import { handleError } from "./lib/handle-error/handle-error.ts";

export type HealthResponse = { status: "ok" };

// An endpoint to check if API is up and running
export const onRequest: PagesFunction = () => {
export const onRequest = handleError(async () => {
return createJsonResponse<HealthResponse>({ status: "ok" });
};
});
22 changes: 22 additions & 0 deletions functions/lib/handle-error/handle-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { ZodError } from "zod";
import { DatabaseException } from "../../db/database-exception.ts";
import { envSchema } from "../../env/env-schema.ts";
import { Bot } from "grammy";

const reportErrorToTelegram = (error: unknown, env: unknown) => {
const envSafe = envSchema.safeParse(env);
if (
!envSafe.success ||
!envSafe.data.BOT_ERROR_REPORTING_TOKEN ||
!envSafe.data.BOT_ERROR_REPORTING_USER_ID
) {
return;
}

const bot = new Bot(envSafe.data.BOT_ERROR_REPORTING_TOKEN);
return bot.api
.sendMessage(
envSafe.data.BOT_ERROR_REPORTING_USER_ID,
JSON.stringify(error, Object.getOwnPropertyNames(error)),
)
.catch((error) => console.error("Telegram error report failed:", error));
};

export function handleError<T>(
callback: (...args: Parameters<PagesFunction>) => Promise<T>,
Expand All @@ -15,6 +36,7 @@ export function handleError<T>(
if (e instanceof ZodError) {
console.error(e.issues);
}
await reportErrorToTelegram(e, args[0].env);
throw e;
}
};
Expand Down
5 changes: 1 addition & 4 deletions src/screens/deck-form/card-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { useMainButton } from "../../lib/telegram/use-main-button.tsx";
import { useDeckFormStore } from "../../store/deck-form-store-context.tsx";
import WebApp from "@twa-dev/sdk";
import { useBackButton } from "../../lib/telegram/use-back-button.tsx";
import {
isFormEmpty,
isFormTouched,
} from "../../lib/mobx-form/form-has-error.ts";
import { isFormEmpty } from "../../lib/mobx-form/form-has-error.ts";

export const CardForm = observer(() => {
const deckFormStore = useDeckFormStore();
Expand Down

0 comments on commit ee060a3

Please sign in to comment.