mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 05:25:40 +09:00
refactor(pkgs): share vitest config and dedupe helpers (#3384)
This commit is contained in:
1 parent
5695510980
commit
575a5adbef
43 files changed
+172
-874
No files matched your search
@@ -1,34 +1,29 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
|
||||
import {BadRequestError} from '@fluxer/errors/src/domains/core/BadRequestError';
|
||||
import {AppErrorHandler} from '@fluxer/errors/src/domains/core/ErrorHandlers';
|
||||
import {AppErrorHandler, type BaseHonoEnv} from '@fluxer/errors/src/domains/core/ErrorHandlers';
|
||||
import {ServiceUnavailableError} from '@fluxer/errors/src/HttpErrors';
|
||||
import type {BaseHonoEnv} from '@fluxer/hono_types/src/HonoTypes';
|
||||
import {Hono} from 'hono';
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
type LogCall = [Record<string, unknown>, string | undefined];
|
||||
|
||||
const logCalls = vi.hoisted(() => ({
|
||||
debug: [] as Array<LogCall>,
|
||||
warn: [] as Array<LogCall>,
|
||||
error: [] as Array<LogCall>,
|
||||
}));
|
||||
|
||||
vi.mock('@fluxer/logger/src/Logger', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@fluxer/logger/src/Logger')>();
|
||||
const record = (bucket: Array<LogCall>) => (obj: Record<string, unknown>, msg?: string) => {
|
||||
bucket.push([obj, msg]);
|
||||
};
|
||||
return {
|
||||
...actual,
|
||||
createLogger: () => ({
|
||||
trace: () => {},
|
||||
debug: record(logCalls.debug),
|
||||
debug: () => {},
|
||||
info: () => {},
|
||||
warn: record(logCalls.warn),
|
||||
error: record(logCalls.error),
|
||||
warn: () => {},
|
||||
error: (obj: Record<string, unknown>, msg?: string) => {
|
||||
logCalls.error.push([obj, msg]);
|
||||
},
|
||||
fatal: () => {},
|
||||
}),
|
||||
};
|
||||
@@ -42,8 +37,6 @@ function createApp(): Hono<BaseHonoEnv> {
|
||||
|
||||
describe('AppErrorHandler logging', () => {
|
||||
beforeEach(() => {
|
||||
logCalls.debug.length = 0;
|
||||
logCalls.warn.length = 0;
|
||||
logCalls.error.length = 0;
|
||||
});
|
||||
|
||||
@@ -75,20 +68,6 @@ describe('AppErrorHandler logging', () => {
|
||||
expect(loggedError.cause).toBe(cause);
|
||||
});
|
||||
|
||||
it('logs 4xx FluxerErrors at debug rather than error', async () => {
|
||||
const app = createApp();
|
||||
app.get('/thing', () => {
|
||||
throw new BadRequestError({code: APIErrorCodes.BAD_REQUEST});
|
||||
});
|
||||
const response = await app.request('/thing');
|
||||
expect(response.status).toBe(400);
|
||||
expect(logCalls.error).toHaveLength(0);
|
||||
expect(logCalls.debug).toHaveLength(1);
|
||||
const [details, message] = logCalls.debug[0]!;
|
||||
expect(message).toBe('Request rejected');
|
||||
expect(details.status).toBe(400);
|
||||
});
|
||||
|
||||
it('logs the matched route pattern instead of the request path', async () => {
|
||||
const app = createApp();
|
||||
app.get('/reset/:token', () => {
|
||||
@@ -99,15 +78,4 @@ describe('AppErrorHandler logging', () => {
|
||||
expect(logCalls.error).toHaveLength(1);
|
||||
expect(logCalls.error[0]![0].path).toBe('/reset/:token');
|
||||
});
|
||||
|
||||
it('still reports unexpected errors as unhandled', async () => {
|
||||
const app = createApp();
|
||||
app.get('/thing', () => {
|
||||
throw new Error('boom');
|
||||
});
|
||||
const response = await app.request('/thing');
|
||||
expect(response.status).toBe(500);
|
||||
expect(logCalls.error).toHaveLength(1);
|
||||
expect(logCalls.error[0]![1]).toBe('Unhandled error occurred');
|
||||
});
|
||||
});
|
||||
@@ -3,9 +3,8 @@
|
||||
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
|
||||
import {Locales} from '@fluxer/constants/src/Locales';
|
||||
import {BadRequestError} from '@fluxer/errors/src/domains/core/BadRequestError';
|
||||
import {AppErrorHandler} from '@fluxer/errors/src/domains/core/ErrorHandlers';
|
||||
import {AppErrorHandler, type BaseHonoEnv} from '@fluxer/errors/src/domains/core/ErrorHandlers';
|
||||
import {getErrorMessageUnsafe} from '@fluxer/errors/src/i18n/ErrorI18n';
|
||||
import type {BaseHonoEnv} from '@fluxer/hono_types/src/HonoTypes';
|
||||
import {Logger} from '@fluxer/logger/src/Logger';
|
||||
import {Hono} from 'hono';
|
||||
import {afterEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
@@ -20,11 +20,27 @@ import {FluxerError} from '@fluxer/errors/src/FluxerError';
|
||||
import {ErrorCodeToI18nKey} from '@fluxer/errors/src/i18n/ErrorCodeMappings';
|
||||
import {getErrorMessageUnsafe} from '@fluxer/errors/src/i18n/ErrorI18n';
|
||||
import type {ErrorI18nKey} from '@fluxer/errors/src/i18n/ErrorI18nMessages';
|
||||
import type {BaseHonoEnv, ErrorI18nService} from '@fluxer/hono_types/src/HonoTypes';
|
||||
import {createLogger} from '@fluxer/logger/src/Logger';
|
||||
import type {Context} from 'hono';
|
||||
import {HTTPException} from 'hono/http-exception';
|
||||
|
||||
interface ErrorI18nService {
|
||||
getMessage(
|
||||
key: string,
|
||||
locale: string | null | undefined,
|
||||
variables?: Record<string, unknown>,
|
||||
fallbackMessage?: string,
|
||||
): string;
|
||||
}
|
||||
|
||||
export interface BaseHonoEnv {
|
||||
Variables: {
|
||||
errorI18nService?: ErrorI18nService;
|
||||
requestLocale?: string;
|
||||
requestId?: string;
|
||||
};
|
||||
}
|
||||
|
||||
const logger = createLogger('errors');
|
||||
const LOCALE_LOOKUP = new Map<string, string>(Object.values(Locales).map((locale) => [locale.toLowerCase(), locale]));
|
||||
|
||||
|
||||
Reference in new issue
Block a user