Skip to content

Commit

Permalink
add a JSONSafeParse function to handle parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktaubeneck committed Jun 28, 2024
1 parent 3751339 commit c446524
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions server/app/query/view/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
initialRunTimeByRemoteServer,
} from "@/app/query/servers";
import { StatsComponent } from "@/app/query/view/[id]/charts";
import { JSONSafeParse } from "@/app/utils";
import { getQuery, Query } from "@/data/query";

export default function QueryPage({ params }: { params: { id: string } }) {
Expand Down Expand Up @@ -58,11 +59,6 @@ export default function QueryPage({ params }: { params: { id: string } }) {
setStatsHidden(!statsHidden);
}


const queryParams = Object.entries(
JSON.parse((query?.params as string) || "{}"),
);

function handleCheckbox(e: React.ChangeEvent<HTMLInputElement>) {
const remoteServer = e.target.id;

Expand All @@ -81,6 +77,10 @@ export default function QueryPage({ params }: { params: { id: string } }) {
}
}

const queryParams = Object.entries(
JSONSafeParse((query?.params as string) || "{}"),
);

const kill = async (remoteServers: RemoteServersType) => {
const query: Query = await getQuery(params.id);

Expand Down
12 changes: 12 additions & 0 deletions server/app/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function JSONSafeParse(s: string) {
try {
return JSON.parse(s);
} catch (error) {
if (error instanceof SyntaxError) {
console.error(`${error}`);
console.error(`Failed to parse JSON from string ${s}`);
return {};
}
throw error;
}
}

0 comments on commit c446524

Please sign in to comment.