优化脚本 API 路由的错误处理逻辑

This commit is contained in:
taozhiyu 2025-08-10 18:35:05 +08:00
parent 37a2b1267d
commit 53a96462b2
No known key found for this signature in database
GPG Key ID: A38E8DF826ECA7E3

View File

@ -32,46 +32,46 @@ export default (app: Router) => {
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
let result: IFile[] = []; let result: IFile[] = [];
const blacklist = [ const blacklist = [
'node_modules', 'node_modules',
'.git', '.git',
'.pnpm', '.pnpm',
'pnpm-lock.yaml', 'pnpm-lock.yaml',
'yarn.lock', 'yarn.lock',
'package-lock.json', 'package-lock.json',
]; ];
if (req.query.path) { if (req.query.path) {
result = await readDir( result = await readDir(
req.query.path as string, req.query.path as string,
config.scriptPath, config.scriptPath,
blacklist, blacklist,
); );
} else { } else {
result = await readDirs( result = await readDirs(
config.scriptPath, config.scriptPath,
config.scriptPath, config.scriptPath,
blacklist, blacklist,
(a, b) => { (a, b) => {
if (a.type === b.type) { if (a.type === b.type) {
return a.title.localeCompare(b.title); return a.title.localeCompare(b.title);
} else { } else {
return a.type === 'directory' ? -1 : 1; return a.type === 'directory' ? -1 : 1;
} }
}, },
); );
}
res.send({
code: 200,
data: result,
});
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
} }
res.send({ });
code: 200,
data: result,
});
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
});
route.get( route.get(
'/detail', '/detail',
@ -109,8 +109,8 @@ export default (app: Router) => {
try { try {
const scriptService = Container.get(ScriptService); const scriptService = Container.get(ScriptService);
const content = await scriptService.getFile( const content = await scriptService.getFile(
req.query.path as string, req.query?.path as string || '',
req.params?.file || '', req.params.file,
); );
res.send({ code: 200, data: content }); res.send({ code: 200, data: content });
} catch (e) { } catch (e) {