fix: add reject error

This commit is contained in:
DIYgod 2024-02-25 21:20:57 +08:00
parent b8a2301590
commit fa7eddb50a
No known key found for this signature in database
4 changed files with 13 additions and 10 deletions

View File

@ -1,5 +1,4 @@
import { type ErrorHandler } from 'hono';
import _RequestInProgressError from './request-in-progress';
import { getDebugInfo, setDebugInfo } from '@/utils/debug-info';
import { config } from '@/config';
import Sentry from '@sentry/node';
@ -8,7 +7,8 @@ import art from 'art-template';
import * as path from 'node:path';
import gitHash from '@/utils/git-hash';
export const RequestInProgressError = _RequestInProgressError;
import RequestInProgressError from './request-in-progress';
import RejectError from './reject';
export const errorHandler: ErrorHandler = (error, ctx) => {
let message = '';
@ -46,7 +46,8 @@ export const errorHandler: ErrorHandler = (error, ctx) => {
ctx.status(503);
message = error.message;
ctx.set('Cache-Control', `public, max-age=${config.cache.requestTimeout}`);
} else if (ctx.res.status === 403) {
} else if (error instanceof RejectError) {
ctx.status(403);
message = error.message;
} else {
ctx.status(404);

3
lib/errors/reject.ts Normal file
View File

@ -0,0 +1,3 @@
class RejectError extends Error {}
export default RejectError;

View File

@ -1,13 +1,12 @@
import type { MiddlewareHandler, Context } from 'hono';
import type { MiddlewareHandler } from 'hono';
import { config } from '@/config';
import md5 from '@/utils/md5';
import isLocalhost from 'is-localhost-ip';
import { getIp } from '@/utils/helpers';
import RejectError from '@/errors/reject';
const reject = (ctx: Context) => {
ctx.status(403);
throw new Error('Authentication failed. Access denied.');
const reject = () => {
throw new RejectError('Authentication failed. Access denied.');
};
const ipv4Pattern = /^(\d{1,3}\.){3}\d{1,3}$/;
@ -67,7 +66,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => {
return grant();
}
reject(ctx);
reject();
}
};

View File

@ -2,7 +2,7 @@ import xxhash from 'xxhash-wasm';
import type { MiddlewareHandler } from 'hono';
import { config } from '@/config';
import { RequestInProgressError } from '@/errors';
import RequestInProgressError from '@/errors/request-in-progress';
import cacheModule from '@/utils/cache/index';
import { Data } from '@/types';