mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
The API worker had no error tracking: the Hono onError handler returned a 500 without
logging, and several R2/email failures were swallowed with `.catch(() => {})`, so a
failing Stripe webhook or a data-plane error went unnoticed.
- onError now logs every 5xx as a structured console.error line (captured by Workers
Observability, already enabled in wrangler.jsonc) AND persists it to a new error_logs
D1 table (migration 0013), so incidents survive the short Workers-Logs retention window
and are queryable from our own database:
wrangler d1 execute finance-manager --remote --env prod \
--command "SELECT id, created_at, status, method, path, message FROM error_logs ORDER BY id DESC LIMIT 50"
4xx client errors are intentional control flow and are not recorded (clean signal). The
D1 write is best-effort (executionCtx.waitUntil) and never affects the response.
- Stop swallowing R2 cleanup deletes (account deletion, receipt upload/replace/delete) and
the account-exists notice email; they now console.error.
- New worker test verifies 5xx rows are persisted with method/path/status/message/user_id/
request_id + stack, and that 4xx is not recorded.
No external service or DSN required. Deploy to dev, trigger an error, confirm a row lands in
error_logs before prod.
75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import { env } from 'cloudflare:test';
|
|
import type { Context } from 'hono';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { logWorkerError } from '../src/errorlog';
|
|
import type { AppEnv } from '../src/index';
|
|
|
|
// Minimal Context stand-in exercising only what logWorkerError touches. We pass the REAL test D1
|
|
// (env.DB, migrations applied) so the persistence path is genuinely verified.
|
|
function makeCtx(
|
|
over: { method?: string; path?: string; ray?: string | null; userId?: number } = {}
|
|
): { ctx: Context<AppEnv>; waited: Promise<unknown>[] } {
|
|
const waited: Promise<unknown>[] = [];
|
|
const ctx = {
|
|
req: {
|
|
method: over.method ?? 'POST',
|
|
path: over.path ?? '/api/transactions',
|
|
header: (_name: string) => (over.ray === undefined ? 'ray-abc' : over.ray),
|
|
},
|
|
get: (_key: string) => over.userId ?? 7,
|
|
env: { DB: env.DB },
|
|
executionCtx: {
|
|
waitUntil: (p: Promise<unknown>) => {
|
|
waited.push(p);
|
|
},
|
|
},
|
|
} as unknown as Context<AppEnv>;
|
|
return { ctx, waited };
|
|
}
|
|
|
|
describe('logWorkerError', () => {
|
|
beforeEach(async () => {
|
|
await env.DB.prepare('DELETE FROM error_logs').run();
|
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
});
|
|
|
|
it('persists 5xx failures to the error_logs table with request context', async () => {
|
|
const { ctx, waited } = makeCtx({ method: 'PUT', path: '/api/transactions/9', userId: 42, ray: 'ray-xyz' });
|
|
logWorkerError(ctx, new Error('kaboom'), 500);
|
|
await Promise.all(waited);
|
|
|
|
const row = await env.DB.prepare(
|
|
'SELECT method, path, status, message, user_id, request_id FROM error_logs ORDER BY id DESC LIMIT 1'
|
|
).first<Record<string, unknown>>();
|
|
|
|
expect(row).toMatchObject({
|
|
method: 'PUT',
|
|
path: '/api/transactions/9',
|
|
status: 500,
|
|
message: 'kaboom',
|
|
user_id: 42,
|
|
request_id: 'ray-xyz',
|
|
});
|
|
});
|
|
|
|
it('does NOT record 4xx client errors (intentional control flow)', async () => {
|
|
const { ctx, waited } = makeCtx();
|
|
logWorkerError(ctx, new Error('bad input'), 400);
|
|
await Promise.all(waited);
|
|
|
|
const c = await env.DB.prepare('SELECT COUNT(*) AS n FROM error_logs').first<{ n: number }>();
|
|
expect(c?.n).toBe(0);
|
|
});
|
|
|
|
it('captures the error stack for 5xx', async () => {
|
|
const { ctx, waited } = makeCtx();
|
|
logWorkerError(ctx, new Error('with-stack'), 503);
|
|
await Promise.all(waited);
|
|
|
|
const row = await env.DB.prepare('SELECT stack FROM error_logs ORDER BY id DESC LIMIT 1').first<{
|
|
stack: string | null;
|
|
}>();
|
|
expect(row?.stack).toContain('with-stack');
|
|
});
|
|
});
|