Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
yangw-dev committed Jan 31, 2025
1 parent 81f4074 commit b66756f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
24 changes: 24 additions & 0 deletions torchci/lib/GeneralUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { Octokit } from "octokit";
import { isFailure } from "./JobClassifierUtil";
import { CommitData, JobData } from "./types";

class ErrorWithStatusCode extends Error {
status: number;
info: any;
constructor(message: string, status: number, info: any) {
super(message);
this.status = status;
this.info = info;
}
}

export function includesCaseInsensitive(
value: string,
pattern: string
Expand All @@ -11,6 +21,20 @@ export function includesCaseInsensitive(

export const fetcher = (url: string) => fetch(url).then((res) => res.json());

export const fetcherHandleError = async (url: string) => {
const res = await fetch(url);
if (!res.ok) {
const info = await res.json();
const error = new ErrorWithStatusCode(
`An error occurred while fetching the data`,
res.status,
info?.error
);
throw error;
}
return res.json();
};

export const getMessage = (
message: string,
classification: string,
Expand Down
34 changes: 17 additions & 17 deletions torchci/lib/error_utils.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// TypeScript defaults error type to unknown, this file is to handle error messages and types

type ErrorWithMessage = {
message: string
}
message: string;
};

function isErrorWithMessage(error: unknown): error is ErrorWithMessage {
return (
typeof error === 'object' &&
error !== null &&
'message' in error &&
typeof (error as Record<string, unknown>).message === 'string'
)
return (
typeof error === "object" &&
error !== null &&
"message" in error &&
typeof (error as Record<string, unknown>).message === "string"
);
}

function toErrorWithMessage(candidate: unknown): ErrorWithMessage {
if (isErrorWithMessage(candidate)) {
return candidate
}
try {
return new Error(JSON.stringify(candidate))
} catch {
return new Error(String(candidate))
}
if (isErrorWithMessage(candidate)) {
return candidate;
}
try {
return new Error(JSON.stringify(candidate));
} catch {
return new Error(String(candidate));
}
}
export function getErrorMessage(error: unknown) {
return toErrorWithMessage(error).message
return toErrorWithMessage(error).message;
}
10 changes: 6 additions & 4 deletions torchci/pages/api/utilization/[workflowId]/[jobId]/[attempt].ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ export default async function handler(
job_id: jobId as string,
};

try{
try {
const utilData = await fetchUtilization(params);
if (utilData == null) {
return res.status(404).json({error: `No data found for params ${JSON.stringify(params)}`});
return res
.status(404)
.json({ error: `No data found for params ${JSON.stringify(params)}` });
}
return res.status(200).json({data: utilData});
return res.status(200).json(utilData);
} catch (error) {
const err_msg = getErrorMessage(error);
return res.status(500).json({error: err_msg});
return res.status(500).json({ error: err_msg });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetcher } from "lib/GeneralUtils";
import { fetcherHandleError } from "lib/GeneralUtils";
import { useRouter } from "next/router";
import useSWR from "swr";

Expand All @@ -8,12 +8,20 @@ const ApiData = () => {

let { data, error } = useSWR(
`/api/utilization/${workflowId}/${jobId}/${attempt}`,
fetcher,
fetcherHandleError,
{
refreshInterval: 12 * 60 * 60 * 1000, // refresh every 12 hours
}
);

if (error) {
return (
<div>
error: {error.message}, StatusCode: {error.status}, info: {error.info}
</div>
);
}

if (!data) {
return <div>loading...</div>;
}
Expand All @@ -23,7 +31,7 @@ const ApiData = () => {
<h1>API Data</h1>
<div>
workflowId:{workflowId}, JobId: {jobId}, attempt: {attempt}, job_name:{" "}
{data.metadata.job_name}, workflow_name: {data.metadata.workflow_name}
{data.metadata?.job_name}, workflow_name: {data.metadata?.workflow_name}
</div>
<div
style={{ maxWidth: "800px", whiteSpace: "pre-wrap", overflowX: "auto" }}
Expand Down

0 comments on commit b66756f

Please sign in to comment.