Skip to content

Commit

Permalink
Merge pull request #30 from Gurubase/task/sentry-errors
Browse files Browse the repository at this point in the history
Add sentry error logging in slug page
  • Loading branch information
fatihbaltaci authored Jan 16, 2025
2 parents b2135e0 + bff232d commit 05be8d2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
33 changes: 25 additions & 8 deletions src/gurubase-frontend/src/app/not-found.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import Link from "next/link";
"use client";

import Link from "next/link";
import { useEffect } from "react";
import { SolarHomeBold } from "@/components/Icons";
import { useAppSelector, useAppDispatch } from "@/redux/hooks";
import { setNotFoundContext } from "@/redux/slices/mainFormSlice";
import { reportErrorToSentry } from "@/utils/sentry";

export default function NotFound() {
// if (
// process.env.NEXT_PUBLIC_SENTRY_AUTH_TOKEN &&
// process.env.NEXT_PUBLIC_NODE_ENV === "production"
// ) {
// Sentry.captureException(new Error("404 - Page Not Found (Root)"));
// }
const dispatch = useAppDispatch();
const notFoundContext = useAppSelector(
(state) => state.mainForm.notFoundContext
);

useEffect(() => {
const reportError = async () => {
if (
process.env.NEXT_PUBLIC_NODE_ENV === "production" &&
notFoundContext
) {
await reportErrorToSentry("404 - Page Not Found", notFoundContext);
dispatch(setNotFoundContext(null));
}
};

reportError();
}, [notFoundContext, dispatch]);

return (
<div className="flex flex-col items-center justify-center min-h-screen bg-gray-100 guru-sm:px-4">
Expand All @@ -28,4 +45,4 @@ export default function NotFound() {
</Link>
</div>
);
}
}
44 changes: 41 additions & 3 deletions src/gurubase-frontend/src/components/ResultClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import * as Sentry from "@sentry/nextjs";
import { notFound, redirect, useRouter } from "next/navigation";
import { useEffect, useRef, useState } from "react";

Expand All @@ -24,7 +25,8 @@ import {
setSlugPageRendered,
setStreamError,
setStreamingStatus,
setWaitingForFirstChunk
setWaitingForFirstChunk,
setNotFoundContext
} from "@/redux/slices/mainFormSlice";
import { bingeRedirection } from "@/utils/bingeRedirection";
import { getStream } from "@/utils/clientActions";
Expand Down Expand Up @@ -348,6 +350,24 @@ export const ResultClient = ({
//console.log("set has fetched to true");
} else {
if (!isInitializing) {
// Set not found context before calling notFound
dispatch(
setNotFoundContext({
errorType: "404",
component: "ResultClient",
reason: `Will try to stream but it is not suitable.`,
question,
description,
prompt_tokens,
completion_tokens,
answer_length,
user_intent,
slug,
dirty,
isInitializing,
hasFetched
})
);
notFound();
}
}
Expand All @@ -369,7 +389,25 @@ export const ResultClient = ({

// if window is undefined it mean it is server side rendering and instantContent is not available so redirect to not found page
if (typeof window === "undefined" && !instantContent && !dirty) {
return redirect("/not-found");
// Set not found context for server-side case
dispatch(
setNotFoundContext({
errorType: "404",
component: "ResultClient",
reason: `No content exists while js is disabled.`,
dirty,
instantContent,
slug,
question,
description,
prompt_tokens,
completion_tokens,
answer_length,
user_intent,
user_question
})
);
notFound();
}

const triggerStreamUpdate = (summaryData) => {
Expand Down Expand Up @@ -427,4 +465,4 @@ export const ResultClient = ({
/>
</main>
);
};
};
11 changes: 8 additions & 3 deletions src/gurubase-frontend/src/redux/slices/mainFormSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const initialState = {
dateUpdated: null,
followUpQuestions: [],
treeData: null,
bingeOutdated: false
bingeOutdated: false,
notFoundContext: null
};

const mainFormSlice = createSlice({
Expand Down Expand Up @@ -178,6 +179,9 @@ const mainFormSlice = createSlice({
},
setBingeMapRefreshTrigger: (state, action) => {
state.bingeMapRefreshTrigger = action.payload;
},
setNotFoundContext: (state, action) => {
state.notFoundContext = action.payload;
}
}
});
Expand Down Expand Up @@ -217,6 +221,7 @@ export const {
resetErrors,
setQuestionUpdate,
setFollowUpQuestions,
setBingeInfo
setBingeInfo,
setNotFoundContext
} = mainFormSlice.actions;
export default mainFormSlice.reducer;
export default mainFormSlice.reducer;
16 changes: 16 additions & 0 deletions src/gurubase-frontend/src/utils/sentry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Sentry from "@sentry/nextjs";

export const reportErrorToSentry = async (error, context) => {
try {
const errorObject = new Error(error);

Sentry.captureException(errorObject, {
tags: {
...context,
environment: process.env.NEXT_PUBLIC_NODE_ENV
}
});
} catch (error) {
// console.error("[Sentry] Failed to report error:", error);
}
};

0 comments on commit 05be8d2

Please sign in to comment.