1
- import { OpenAPIParseError , parseOpenAPI } from '@gitbook/openapi-parser' ;
1
+ import {
2
+ type Filesystem ,
3
+ OpenAPIParseError ,
4
+ type OpenAPIParseErrorCode ,
5
+ parseOpenAPI ,
6
+ } from '@gitbook/openapi-parser' ;
2
7
3
- import { noCacheFetchOptions } from '@/lib/data' ;
8
+ import { DataFetcherError , noCacheFetchOptions } from '@/lib/data' ;
4
9
import { resolveContentRef } from '@/lib/references' ;
5
10
import { unstable_cacheLife as cacheLife } from 'next/cache' ;
6
11
import { assert } from 'ts-essentials' ;
@@ -48,7 +53,17 @@ export async function fetchOpenAPIFilesystem(
48
53
} ;
49
54
}
50
55
51
- const fetchFilesystem = async ( url : string ) => {
56
+ const fetchFilesystem = async (
57
+ url : string
58
+ ) : Promise <
59
+ | Filesystem
60
+ | {
61
+ error : {
62
+ code : OpenAPIParseErrorCode ;
63
+ message : string ;
64
+ } ;
65
+ }
66
+ > => {
52
67
'use cache' ;
53
68
try {
54
69
return await fetchFilesystemUncached ( url ) ;
@@ -60,23 +75,16 @@ const fetchFilesystem = async (url: string) => {
60
75
if ( error instanceof OpenAPIParseError ) {
61
76
return { error : { code : error . code , message : error . message } } ;
62
77
}
63
- if ( error instanceof OpenAPIFetchError ) {
78
+ if ( error instanceof DataFetcherError ) {
64
79
return { error : { code : 'invalid' as const , message : 'Failed to fetch OpenAPI file' } } ;
65
80
}
66
- // If the error is not an OpenAPIParseError or OpenAPIFetchError ,
81
+ // If the error is not an OpenAPIParseError or DataFetcherError ,
67
82
// we assume it's an unknown error and return a generic error.
68
83
console . error ( 'Unknown error while fetching OpenAPI file:' , error ) ;
69
84
return { error : { code : 'invalid' as const , message : 'Unknown error' } } ;
70
85
}
71
86
} ;
72
87
73
- class OpenAPIFetchError extends Error {
74
- constructor ( message : string ) {
75
- super ( message ) ;
76
- this . name = 'OpenAPIFetchError' ;
77
- }
78
- }
79
-
80
88
async function fetchFilesystemUncached (
81
89
url : string ,
82
90
options ?: {
@@ -92,9 +100,7 @@ async function fetchFilesystemUncached(
92
100
} ) ;
93
101
94
102
if ( ! response . ok ) {
95
- throw new OpenAPIFetchError (
96
- `Failed to fetch OpenAPI file: ${ response . status } ${ response . statusText } `
97
- ) ;
103
+ throw new DataFetcherError ( 'Failed to fetch OpenAPI file' , response . status ) ;
98
104
}
99
105
100
106
const text = await response . text ( ) ;
0 commit comments