Skip to content

Commit 580f7ad

Browse files
authored
Better debugging of GitBook v1s revalidate endpoint. (#3097)
1 parent cdffd7c commit 580f7ad

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

.changeset/old-rats-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Improve the error message returned by the revalidate endpoint.

packages/gitbook/src/app/(global)/~gitbook/revalidate/route.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ interface JsonBody {
1414
* The body should be a JSON with { tags: string[] }
1515
*/
1616
export async function POST(req: NextRequest) {
17-
const json = (await req.json()) as JsonBody;
17+
let json: JsonBody;
18+
19+
try {
20+
json = await req.json();
21+
} catch (err) {
22+
return NextResponse.json({
23+
error: `invalid json body: ${err}`,
24+
});
25+
}
1826

1927
if (!json.tags || !Array.isArray(json.tags)) {
2028
return NextResponse.json(
@@ -32,16 +40,12 @@ export async function POST(req: NextRequest) {
3240
stats: result.stats,
3341
});
3442
} catch (err: unknown) {
35-
const message = [
36-
err instanceof Error ? err.name : 'Internal Server Error',
37-
...(err instanceof Error && err.message ? [err.message] : []),
38-
...(err instanceof Error && err.cause ? [err.cause] : []),
39-
...(err instanceof Error && err.stack ? [err.stack] : []),
40-
].join('\n');
41-
42-
return new NextResponse(null, {
43-
status: 500,
44-
statusText: message,
45-
});
43+
return NextResponse.json(
44+
{
45+
error: `unexpected error ${err}`,
46+
stack: err instanceof Error ? err.stack : null,
47+
},
48+
{ status: 500 }
49+
);
4650
}
4751
}

packages/gitbook/src/lib/cache/revalidateTags.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,25 @@ export async function revalidateTags(tags: string[]): Promise<{
3636

3737
await Promise.all(
3838
cacheBackends.map(async (backend, backendIndex) => {
39-
const { entries: addedEntries } = await backend.revalidateTags(tags);
39+
try {
40+
const { entries: addedEntries } = await backend.revalidateTags(tags);
4041

41-
addedEntries.forEach(({ key, tag }) => {
42-
stats[key] = stats[key] ?? {
43-
tag,
44-
backends: {},
45-
};
46-
stats[key].backends[backend.name] = { set: true };
42+
addedEntries.forEach(({ key, tag }) => {
43+
stats[key] = stats[key] ?? {
44+
tag,
45+
backends: {},
46+
};
47+
stats[key].backends[backend.name] = { set: true };
4748

48-
entries.set(key, { tag, key });
49-
keysByBackend.set(backendIndex, [...(keysByBackend.get(backendIndex) ?? []), key]);
50-
});
49+
entries.set(key, { tag, key });
50+
keysByBackend.set(backendIndex, [
51+
...(keysByBackend.get(backendIndex) ?? []),
52+
key,
53+
]);
54+
});
55+
} catch (err) {
56+
throw new Error(`error revalidating tags on backend ${backend.name}: ${err}`);
57+
}
5158
})
5259
);
5360

@@ -71,7 +78,8 @@ export async function revalidateTags(tags: string[]): Promise<{
7178
return;
7279
}
7380
}
74-
throw error;
81+
82+
throw new Error(`error deleting entries on backend ${backend.name}: ${error}`);
7583
}
7684
}
7785
})

0 commit comments

Comments
 (0)