Skip to content

Commit 38753f7

Browse files
author
Nicolas Dorseuil
committed
review fix
1 parent 7a69b49 commit 38753f7

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

packages/gitbook/src/lib/openapi/fetch.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
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';
27

3-
import { noCacheFetchOptions } from '@/lib/data';
8+
import { DataFetcherError, noCacheFetchOptions } from '@/lib/data';
49
import { resolveContentRef } from '@/lib/references';
510
import { unstable_cacheLife as cacheLife } from 'next/cache';
611
import { assert } from 'ts-essentials';
@@ -48,7 +53,17 @@ export async function fetchOpenAPIFilesystem(
4853
};
4954
}
5055

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+
> => {
5267
'use cache';
5368
try {
5469
return await fetchFilesystemUncached(url);
@@ -60,23 +75,16 @@ const fetchFilesystem = async (url: string) => {
6075
if (error instanceof OpenAPIParseError) {
6176
return { error: { code: error.code, message: error.message } };
6277
}
63-
if (error instanceof OpenAPIFetchError) {
78+
if (error instanceof DataFetcherError) {
6479
return { error: { code: 'invalid' as const, message: 'Failed to fetch OpenAPI file' } };
6580
}
66-
// If the error is not an OpenAPIParseError or OpenAPIFetchError,
81+
// If the error is not an OpenAPIParseError or DataFetcherError,
6782
// we assume it's an unknown error and return a generic error.
6883
console.error('Unknown error while fetching OpenAPI file:', error);
6984
return { error: { code: 'invalid' as const, message: 'Unknown error' } };
7085
}
7186
};
7287

73-
class OpenAPIFetchError extends Error {
74-
constructor(message: string) {
75-
super(message);
76-
this.name = 'OpenAPIFetchError';
77-
}
78-
}
79-
8088
async function fetchFilesystemUncached(
8189
url: string,
8290
options?: {
@@ -92,9 +100,7 @@ async function fetchFilesystemUncached(
92100
});
93101

94102
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);
98104
}
99105

100106
const text = await response.text();

packages/openapi-parser/src/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ErrorObject } from '@scalar/openapi-parser';
22

3-
type OpenAPIParseErrorCode =
3+
export type OpenAPIParseErrorCode =
44
| 'invalid'
55
| 'parse-v2-in-v3'
66
| 'v2-conversion'

0 commit comments

Comments
 (0)