File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed
packages/gitbook/src/lib/openapi Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change 1
- import { parseOpenAPI } from '@gitbook/openapi-parser' ;
1
+ import { OpenAPIParseError , parseOpenAPI } from '@gitbook/openapi-parser' ;
2
2
3
3
import { noCacheFetchOptions } from '@/lib/data' ;
4
4
import { resolveContentRef } from '@/lib/references' ;
5
+ import { unstable_cacheLife as cacheLife } from 'next/cache' ;
5
6
import { assert } from 'ts-essentials' ;
6
7
import { enrichFilesystem } from './enrich' ;
7
8
import type {
@@ -37,6 +38,10 @@ export async function fetchOpenAPIFilesystem(
37
38
return fetchFilesystem ( resolved . href ) ;
38
39
} ) ( ) ;
39
40
41
+ if ( 'error' in filesystem ) {
42
+ throw new OpenAPIParseError ( filesystem . error . message , { code : filesystem . error . code } ) ;
43
+ }
44
+
40
45
return {
41
46
filesystem,
42
47
specUrl : resolved . href ,
@@ -45,7 +50,17 @@ export async function fetchOpenAPIFilesystem(
45
50
46
51
const fetchFilesystem = async ( url : string ) => {
47
52
'use cache' ;
48
- return fetchFilesystemUncached ( url ) ;
53
+ try {
54
+ return await fetchFilesystemUncached ( url ) ;
55
+ } catch ( error ) {
56
+ // Throwing an error inside a "use cache" function obfuscates the error,
57
+ // so we need to handle it here and recreates the error outside the cache function.
58
+ if ( error instanceof OpenAPIParseError ) {
59
+ cacheLife ( 'seconds' ) ;
60
+ return { error : { code : error . code , message : error . message } } ;
61
+ }
62
+ throw error ;
63
+ }
49
64
} ;
50
65
51
66
async function fetchFilesystemUncached (
Original file line number Diff line number Diff line change 1
1
import { fetchOpenAPIFilesystem } from '@/lib/openapi/fetch' ;
2
- import type { OpenAPIParseError } from '@gitbook/openapi-parser' ;
2
+ import { OpenAPIParseError } from '@gitbook/openapi-parser' ;
3
3
import { type OpenAPIOperationData , resolveOpenAPIOperation } from '@gitbook/react-openapi' ;
4
4
import type {
5
5
AnyOpenAPIOperationsBlock ,
@@ -55,8 +55,8 @@ async function baseResolveOpenAPIOperationBlock(
55
55
56
56
return { data, specUrl } ;
57
57
} catch ( error ) {
58
- if ( error instanceof Error && error . name === ' OpenAPIParseError' ) {
59
- return { error : error as OpenAPIParseError } ;
58
+ if ( error instanceof OpenAPIParseError ) {
59
+ return { error } ;
60
60
}
61
61
62
62
throw error ;
You can’t perform that action at this time.
0 commit comments