File tree Expand file tree Collapse file tree 3 files changed +40
-23
lines changed
app/(global)/~gitbook/revalidate Expand file tree Collapse file tree 3 files changed +40
-23
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " gitbook " : patch
3
+ ---
4
+
5
+ Improve the error message returned by the revalidate endpoint.
Original file line number Diff line number Diff line change @@ -14,7 +14,15 @@ interface JsonBody {
14
14
* The body should be a JSON with { tags: string[] }
15
15
*/
16
16
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
+ }
18
26
19
27
if ( ! json . tags || ! Array . isArray ( json . tags ) ) {
20
28
return NextResponse . json (
@@ -32,16 +40,12 @@ export async function POST(req: NextRequest) {
32
40
stats : result . stats ,
33
41
} ) ;
34
42
} 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
+ ) ;
46
50
}
47
51
}
Original file line number Diff line number Diff line change @@ -36,18 +36,25 @@ export async function revalidateTags(tags: string[]): Promise<{
36
36
37
37
await Promise . all (
38
38
cacheBackends . map ( async ( backend , backendIndex ) => {
39
- const { entries : addedEntries } = await backend . revalidateTags ( tags ) ;
39
+ try {
40
+ const { entries : addedEntries } = await backend . revalidateTags ( tags ) ;
40
41
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 } ;
47
48
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
+ }
51
58
} )
52
59
) ;
53
60
@@ -71,7 +78,8 @@ export async function revalidateTags(tags: string[]): Promise<{
71
78
return ;
72
79
}
73
80
}
74
- throw error ;
81
+
82
+ throw new Error ( `error deleting entries on backend ${ backend . name } : ${ error } ` ) ;
75
83
}
76
84
}
77
85
} )
You can’t perform that action at this time.
0 commit comments