Skip to content

Commit

Permalink
fix(hub): add better error handling for paths that cant be found in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
BrownBrownBear committed Dec 23, 2024
1 parent 4f20c75 commit a4fa7f9
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions apps/hub/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "../styles/globals.css";
import { readFileSync } from "fs";
import { existsSync, readFileSync } from "fs";
import path from "path";
import { Metadata } from "next";
import dynamic from "next/dynamic";
Expand Down Expand Up @@ -38,14 +38,38 @@ const PostHogPageView = dynamic(() => import("./PostHogPageView"), {
});

export default async function RootLayout(props: { children: React.ReactNode }) {
const fetchedTokenList = await (tokenListUrl.startsWith("http")
? fetch(tokenListUrl).then((res) => res.json())
: JSON.parse(
readFileSync(
path.join(process.cwd(), `public/${tokenListUrl}`),
"utf8",
),
));
let fetchedTokenList = null;

try {
if (tokenListUrl.startsWith("http")) {
const response = await fetch(tokenListUrl);
if (!response.ok) {
throw new Error(`Failed to fetch token list: ${response.statusText}`);
}
fetchedTokenList = await response.json();
} else {
const publicPath = path.join(process.cwd(), "publica");
const tokenListPath = path.join(publicPath, tokenListUrl);

// Check if public directory and file exist
if (existsSync(tokenListPath)) {
try {
const fileContent = readFileSync(tokenListPath, "utf8");
fetchedTokenList = JSON.parse(fileContent);
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
console.error(`Error parsing token list file: ${errorMessage}`);
}
} else {
console.error(`Token list file not found at: ${tokenListPath}`);
}
}
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
console.error(`Error loading token list: ${errorMessage}`);
}

return (
<html lang="en" className="bg-background">
Expand Down

0 comments on commit a4fa7f9

Please sign in to comment.