fix: not found error handler

This commit is contained in:
DIYgod
2024-02-26 19:34:17 +08:00
parent b58b6e60cf
commit 206782bb61
4 changed files with 13 additions and 3 deletions
+2 -1
View File
@@ -13,7 +13,7 @@ import parameter from '@/middleware/parameter';
import logger from '@/utils/logger';
import routes from '@/routes';
import { errorHandler } from '@/errors';
import { notFoundHandler, errorHandler } from '@/errors';
process.on('uncaughtException', (e) => {
logger.error('uncaughtException: ' + e);
@@ -32,6 +32,7 @@ app.use('*', cache);
routes(app);
app.notFound(notFoundHandler);
app.onError(errorHandler);
export default app;
+1 -1
View File
@@ -72,7 +72,7 @@ export type Config = {
model?: string;
temperature?: number;
maxTokens?: number;
endpoint?: string;
endpoint: string;
prompt?: string;
};
bilibili: {
+7 -1
View File
@@ -1,4 +1,4 @@
import { type ErrorHandler } from 'hono';
import { type NotFoundHandler, type ErrorHandler } from 'hono';
import { getDebugInfo, setDebugInfo } from '@/utils/debug-info';
import { config } from '@/config';
import Sentry from '@sentry/node';
@@ -9,6 +9,7 @@ import gitHash from '@/utils/git-hash';
import RequestInProgressError from './request-in-progress';
import RejectError from './reject';
import NotFoundError from './not-found';
export const errorHandler: ErrorHandler = (error, ctx) => {
let message = '';
@@ -49,6 +50,9 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
} else if (error instanceof RejectError) {
ctx.status(403);
message = error.message;
} else if (error instanceof NotFoundError) {
ctx.status(404);
message = 'wrong path';
} else {
ctx.status(404);
}
@@ -66,3 +70,5 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
);
}
};
export const notFoundHandler: NotFoundHandler = (ctx) => errorHandler(new NotFoundError(), ctx);
+3
View File
@@ -0,0 +1,3 @@
class NotFoundError extends Error {}
export default NotFoundError;