diff --git a/lib/app.ts b/lib/app.ts index 7a887eec6..fbf34f50d 100644 --- a/lib/app.ts +++ b/lib/app.ts @@ -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; diff --git a/lib/config.ts b/lib/config.ts index 195fb5805..b017dc879 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -72,7 +72,7 @@ export type Config = { model?: string; temperature?: number; maxTokens?: number; - endpoint?: string; + endpoint: string; prompt?: string; }; bilibili: { diff --git a/lib/errors/index.ts b/lib/errors/index.ts index a16cfe091..109b1c618 100644 --- a/lib/errors/index.ts +++ b/lib/errors/index.ts @@ -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); diff --git a/lib/errors/not-found.ts b/lib/errors/not-found.ts new file mode 100644 index 000000000..2ba6e18b3 --- /dev/null +++ b/lib/errors/not-found.ts @@ -0,0 +1,3 @@ +class NotFoundError extends Error {} + +export default NotFoundError;