Skip to content

Commit 6b80077

Browse files
CopilotSoonIter
andauthored
chore(auto-nav-sidebar): Improve missing page error readability (#2880)
Co-authored-by: SoonIter <[email protected]>
1 parent f4c42db commit 6b80077

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/core/src/node/auto-nav-sidebar/normalize.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
slash,
1010
} from '@rspress/shared';
1111
import { logger } from '@rspress/shared/logger';
12+
import picocolors from 'picocolors';
1213
import { PUBLIC_DIR } from '../constants';
1314
import { absolutePathToRoutePath, addRoutePrefix } from '../route/RoutePage';
1415
import { createError } from '../utils';
@@ -27,6 +28,8 @@ import {
2728
readJson,
2829
} from './utils';
2930

31+
const AUTO_NAV_SIDEBAR_ERROR_PREFIX = '[auto-nav-sidebar] ';
32+
3033
function getFileKey(realPath: string | undefined, docsDir: string) {
3134
return realPath
3235
? slash(relative(docsDir, realPath).replace(extname(realPath), ''))
@@ -42,10 +45,11 @@ async function fsDirToMetaItems(
4245
try {
4346
subItems = await readdir(workDir);
4447
} catch (e) {
45-
logger.error(
46-
`Failed to read directory: ${workDir}, maybe it does not exist. Please check it in "_meta.json".`,
48+
const metaFilePath = join(workDir, '_meta.json');
49+
50+
throw createError(
51+
`${AUTO_NAV_SIDEBAR_ERROR_PREFIX}Failed to read directory: ${picocolors.yellow(workDir)}, maybe it does not exist. Please check it in ${picocolors.cyan(metaFilePath)}. (${e})`,
4752
);
48-
throw e;
4953
}
5054
// If there exists a file with the same name of the directory folder
5155
// we don't need to generate SideMeta for this single file
@@ -152,7 +156,7 @@ async function metaItemToSidebarItem(
152156
}
153157

154158
throw createError(
155-
`Unknown meta item type: ${(metaItem as any).type}, please check it in "${join(workDir, '_meta.json')}".`,
159+
`${AUTO_NAV_SIDEBAR_ERROR_PREFIX}Unknown meta item type: ${(metaItem as any).type}, please check it in "${join(workDir, '_meta.json')}".`,
156160
);
157161
}
158162

@@ -170,7 +174,7 @@ async function detectFilePath(absolutePath: string, extensions: string[]) {
170174
}
171175

172176
throw createError(
173-
`The file extension "${ext}" is not supported, please use one of the following extensions: ${extensions.join(', ')}`,
177+
`${AUTO_NAV_SIDEBAR_ERROR_PREFIX}The file extension "${ext}" is not supported, please use one of the following extensions: ${extensions.join(', ')}`,
174178
);
175179
}
176180

@@ -192,10 +196,11 @@ async function metaFileItemToSidebarItem(
192196
}
193197

194198
const { name, context, label, overviewHeaders, tag } = metaItem;
199+
const metaFilePath = join(workDir, '_meta.json');
195200

196201
if (typeof name !== 'string') {
197202
throw createError(
198-
`The file name "${name}" is not a string, please check it in "${join(workDir, '_meta.json')}".`,
203+
`${AUTO_NAV_SIDEBAR_ERROR_PREFIX}The file name "${name}" is not a string, please check it in ${picocolors.cyan(metaFilePath)}.`,
199204
);
200205
}
201206

@@ -206,7 +211,9 @@ async function metaFileItemToSidebarItem(
206211
absolutePathWithExt = await detectFilePath(absolutePath, extensions);
207212
} catch {
208213
throw createError(
209-
`The file "${absolutePath}" does not exist, please check it in "${join(workDir, '_meta.json')}".`,
214+
`${AUTO_NAV_SIDEBAR_ERROR_PREFIX}${picocolors.white('Missing page file:')} ${picocolors.yellow(
215+
`"${absolutePath}"`,
216+
)}\n${picocolors.white('Please check')} ${picocolors.cyan(join(workDir, '_meta.json'))}.`,
210217
);
211218
}
212219

@@ -465,7 +472,7 @@ function metaCustomLinkItemToSidebarItem(
465472

466473
const { label } = metaItem;
467474
throw createError(
468-
`The custom link "${label}" does not have a link, please check it in "${join(workDir, '_meta.json')}".`,
475+
`${AUTO_NAV_SIDEBAR_ERROR_PREFIX}The custom link "${label}" does not have a link, please check it in "${join(workDir, '_meta.json')}".`,
469476
);
470477
}
471478

packages/core/src/node/utils/error.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import picocolors from 'picocolors';
2+
13
/**
24
* Creates an Error with a truncated stack trace.
35
* Only keeps the first N lines of the stack trace to reduce noise.
@@ -11,7 +13,10 @@ export function createError(message: string, maxStackLines = 5): Error {
1113
if (error.stack) {
1214
const lines = error.stack.split('\n');
1315
if (lines.length > maxStackLines + 1) {
14-
error.stack = `${lines.slice(0, maxStackLines + 1).join('\n')}\n ... (truncated)`;
16+
error.stack = `${lines
17+
.slice(0, maxStackLines + 1)
18+
.map(line => picocolors.gray(line))
19+
.join('\n')}\n ${picocolors.gray('... (truncated)')}`;
1520
}
1621
}
1722
return error;

0 commit comments

Comments
 (0)