fix: add reject error
This commit is contained in:
parent
b8a2301590
commit
fa7eddb50a
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
class RejectError extends Error {}
|
||||
|
||||
export default RejectError;
|
||||
|
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue