Skip to content

Commit

Permalink
return failure reason when revalidation fails (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Sep 16, 2023
1 parent 0bc6b9f commit bb34f68
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/ui/fe-bundle/src/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ const handler: NextApiHandler = async (req, res) => {
console.log(`Found ${pathsToRevalidate.length} paths to revalidate`);

const revalidated: string[] = [];
const failures: string[] = [];
const failures: RevalidateFailure[] = [];
await Promise.all(
pathsToRevalidate.map(async (path) => {
const didSucceed = await tryRevalidate(res, path);
if (didSucceed) {
const response = await tryRevalidate(res, path);
if (response.type === "success") {
revalidated.push(path);
} else {
failures.push(path);
failures.push(response);
}
})
);
Expand All @@ -110,14 +110,30 @@ const handler: NextApiHandler = async (req, res) => {
}
};

async function tryRevalidate(res: NextApiResponse, path: string): Promise<boolean> {
type RevalidateResponse = RevalidateSuccess | RevalidateFailure;

interface RevalidateSuccess {
type: "success";
}

interface RevalidateFailure {
type: "failure";
error: string;
}

async function tryRevalidate(res: NextApiResponse, path: string): Promise<RevalidateResponse> {
try {
await res.revalidate(path);
return true;
return {
type: "success",
};
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
return false;
return {
type: "failure",
error: (err as Error)?.message ?? "No error message",
};
}
}

Expand Down

1 comment on commit bb34f68

@vercel
Copy link

@vercel vercel bot commented on bb34f68 Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

fern-dev – ./packages/ui/fe-bundle

fern-dev-buildwithfern.vercel.app
fern-dev-git-main-buildwithfern.vercel.app
app-dev.buildwithfern.com
devtest.buildwithfern.com

Please sign in to comment.