Skip to content

Commit

Permalink
feat: Added error tracking / logging (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
YohanTz authored Jun 21, 2024
1 parent 3b293be commit c45eeca
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
3 changes: 2 additions & 1 deletion apps/web/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* for Docker builds.
*/
await import("./env.mjs");
import { withLogtail } from "@logtail/next";

/** @type {import("next").NextConfig} */
const config = {
Expand Down Expand Up @@ -35,4 +36,4 @@ const config = {
return config;
},
};
export default config;
export default withLogtail(config);
2 changes: 2 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
},
"dependencies": {
"@headlessui/react": "1.7.10",
"@hotjar/browser": "^1.0.9",
"@logtail/next": "^0.1.5",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.4",
"@radix-ui/react-tabs": "^1.0.4",
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/app/(routes)/root-layout-container.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import Hotjar from "@hotjar/browser";
import clsx from "clsx";
import { cn } from "design-system/src/lib/utils";
import { usePathname } from "next/navigation";
Expand All @@ -8,10 +9,16 @@ import React, { type PropsWithChildren } from "react";
import { api } from "~/utils/api";

import Header from "../_components/Header";
import Logger from "../_components/Logger";
import MobilePlaceholder from "../_components/MobilePlaceholder";
import useCurrentChain from "../_hooks/useCurrentChain";
import Providers from "./providers";

const siteId = 5032874;
const hotjarVersion = 6;

Hotjar.init(siteId, hotjarVersion);

function RootLayoutContainer({ children }: PropsWithChildren) {
const { targetChain } = useCurrentChain();
const pathname = usePathname();
Expand All @@ -35,6 +42,7 @@ function RootLayoutContainer({ children }: PropsWithChildren) {
<MobilePlaceholder />
</div>
)}
<Logger />
</body>
);
}
Expand Down
31 changes: 31 additions & 0 deletions apps/web/src/app/_components/Logger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import { log } from "@logtail/next";
import { useEffect } from "react";

export default function Logger() {
useEffect(() => {
function logError(event: ErrorEvent) {
console.log("ERROR DETECTED");
log.error(event.message, {
column: event.colno,
line: event.lineno,
url: event.filename,
});
}
function logUnhandledRejection(event: PromiseRejectionEvent) {
console.log("ERROR DETECTED");
log.error("Unhandled Promise Rejection", {
reason: event.reason as string,
});
}

window.addEventListener("error", logError);
window.addEventListener("unhandledrejection", logUnhandledRejection);
return () => {
document.removeEventListener("error", logError);
};
}, []);

return null;
}
35 changes: 23 additions & 12 deletions apps/web/src/pages/api/trpc/[trpc].ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { log } from "@logtail/next";
import { withLogtail } from "@logtail/next";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import { env } from "~/../env.mjs";

import { appRouter } from "~/server/api/root";
import { createTRPCContext } from "~/server/api/trpc";

// export API handler
export default createNextApiHandler({
createContext: createTRPCContext,
onError:
env.NODE_ENV === "development"
? ({ error, path }) => {
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`
);
}
: undefined,
router: appRouter,
});
export default withLogtail(
createNextApiHandler({
createContext: createTRPCContext,
onError:
env.NODE_ENV === "development"
? ({ error, path }) => {
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`
);
}
: ({ error, path }) => {
log.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`
);
if (error.stack !== undefined) {
log.error(error.stack);
}
},
router: appRouter,
})
);
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c45eeca

Please sign in to comment.