feat : add sentry

This commit is contained in:
GuillaumeSD
2024-12-29 17:24:21 +01:00
parent d5a1a03ffd
commit 011a1c21bf
8 changed files with 2688 additions and 65 deletions

View File

@@ -6,6 +6,7 @@ import {
LichessGame,
LichessResponse,
} from "@/types/lichess";
import { logErrorToSentry } from "./sentry";
export const getLichessEval = async (
fen: string,
@@ -52,7 +53,8 @@ export const getLichessEval = async (
lines: linesToKeep,
};
} catch (error) {
console.error(error);
const err = error instanceof Error ? error : new Error("Unknown error");
logErrorToSentry(err, { fen, multiPv });
return {
bestMove: "",
lines: [],

17
src/lib/sentry.ts Normal file
View File

@@ -0,0 +1,17 @@
import * as Sentry from "@sentry/nextjs";
export const isSentryEnabled = () =>
!!process.env.NEXT_PUBLIC_SENTRY_DSN && Sentry.isInitialized();
export const logErrorToSentry = (
error: Error,
context?: Record<string, unknown>
) => {
if (isSentryEnabled()) {
Sentry.captureException(error, {
extra: context,
});
} else {
console.error(error);
}
};

View File

@@ -15,6 +15,7 @@ import {
Typography,
Grid2 as Grid,
} from "@mui/material";
import { setContext as setSentryContext } from "@sentry/react";
import { Chess } from "chess.js";
import { useState } from "react";
import GamePgnInput from "./gamePgnInput";
@@ -43,6 +44,7 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
try {
const gameToAdd = getGameFromPgn(pgn);
setSentryContext("loadedGame", { pgn });
if (setGame) {
setGame(gameToAdd);