|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `ObjectLogger` prints an error's `cause` chain. |
| 5 | + * |
| 6 | + * `logger.error(message, err)` used to record `{ message, stack }` of the |
| 7 | + * error alone, so a wrapper thrown with the original on `cause` logged |
| 8 | + * without its root cause — the 5xx fault line (`logServerFault`, |
| 9 | + * `@objectstack/types`) hands its injected logger exactly such a wrapper. |
| 10 | + * The record now carries `error.causes`, bounded, in the same shape the fault |
| 11 | + * log's console fallback prints; the parity block below pins the two copies |
| 12 | + * to one output. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 16 | +import { faultCauseChain } from '@objectstack/types'; |
| 17 | + |
| 18 | +import { createLogger } from './logger.js'; |
| 19 | + |
| 20 | +const ROOT = 'SQLITE_IOERR: disk I/O error reading policy_revision.version'; |
| 21 | +const MID = 'driver read failed for policy_revision'; |
| 22 | +const OUTER = 'The related records of policy could not be read.'; |
| 23 | + |
| 24 | +const wrapped = () => new Error(OUTER, { cause: new Error(MID, { cause: new Error(ROOT) }) }); |
| 25 | + |
| 26 | +type ErrorRecord = { error?: { message: string; stack?: string; causes?: Array<{ message: string; stack?: string }> } }; |
| 27 | + |
| 28 | +/** One `error`-level record as the JSON format writes it to stderr. */ |
| 29 | +function recordOf(log: (logger: ReturnType<typeof createLogger>) => void): ErrorRecord { |
| 30 | + const chunks: string[] = []; |
| 31 | + vi.spyOn(process.stderr, 'write').mockImplementation(((chunk: unknown) => { |
| 32 | + chunks.push(String(chunk)); |
| 33 | + return true; |
| 34 | + }) as never); |
| 35 | + log(createLogger({ format: 'json', level: 'debug' })); |
| 36 | + const lines = chunks.join('').split('\n').filter(Boolean); |
| 37 | + expect(lines).toHaveLength(1); |
| 38 | + return JSON.parse(lines[0]) as ErrorRecord; |
| 39 | +} |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + vi.restoreAllMocks(); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('ObjectLogger — an error record carries the cause chain', () => { |
| 46 | + it('logs each cause\'s message and stack, outermost first', () => { |
| 47 | + const record = recordOf((logger) => logger.error('[5xx] 500 GET /api/v1/x — boom', wrapped(), { status: 500 })); |
| 48 | + expect(record.error?.message).toBe(OUTER); |
| 49 | + expect(record.error?.stack).toContain(OUTER); |
| 50 | + const causes = record.error?.causes ?? []; |
| 51 | + expect(causes).toHaveLength(2); |
| 52 | + expect(causes[0].message).toBe(`Error: ${MID}`); |
| 53 | + expect(causes[0].stack).toContain(MID); |
| 54 | + expect(causes[1].message).toBe(`Error: ${ROOT}`); |
| 55 | + expect(causes[1].stack).toContain(ROOT); |
| 56 | + }); |
| 57 | + |
| 58 | + it('fatal renders it too', () => { |
| 59 | + const record = recordOf((logger) => logger.fatal('down', wrapped())); |
| 60 | + expect(record.error?.causes?.[1].message).toBe(`Error: ${ROOT}`); |
| 61 | + }); |
| 62 | + |
| 63 | + it('CONTROL — an error with no cause carries no causes field', () => { |
| 64 | + const record = recordOf((logger) => logger.error('plain', new Error(OUTER))); |
| 65 | + expect(record.error).toBeDefined(); |
| 66 | + expect(record.error).not.toHaveProperty('causes'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('stops a cycle at its first repeat, and says so', () => { |
| 70 | + const a = new Error('a'); |
| 71 | + const b = new Error('b', { cause: a }); |
| 72 | + (a as { cause?: unknown }).cause = b; |
| 73 | + const record = recordOf((logger) => logger.error('cycle', a)); |
| 74 | + expect(record.error?.causes?.map((c) => c.message)).toEqual([ |
| 75 | + 'Error: b', |
| 76 | + '[cause chain cycle: this link repeats an earlier one]', |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('stops a deep chain after five links, and says so', () => { |
| 81 | + let error = new Error('link 0'); |
| 82 | + for (let i = 1; i <= 20; i += 1) error = new Error(`link ${i}`, { cause: error }); |
| 83 | + const causes = recordOf((logger) => logger.error('deep', error)).error?.causes ?? []; |
| 84 | + expect(causes).toHaveLength(6); |
| 85 | + expect(causes[5]).toEqual({ message: '[cause chain truncated after 5 links]' }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('parity — the logger and the fault log render one shape', () => { |
| 90 | + const cycle = () => { |
| 91 | + const a = new Error('a'); |
| 92 | + (a as { cause?: unknown }).cause = new Error('b', { cause: a }); |
| 93 | + return a; |
| 94 | + }; |
| 95 | + const deep = () => { |
| 96 | + let error = new Error('link 0'); |
| 97 | + for (let i = 1; i <= 9; i += 1) error = new Error(`link ${i}`, { cause: error }); |
| 98 | + return error; |
| 99 | + }; |
| 100 | + const fixtures: Array<[string, () => Error]> = [ |
| 101 | + ['a nested Error chain', wrapped], |
| 102 | + ['a cycle', cycle], |
| 103 | + ['a deep chain', deep], |
| 104 | + ['a string cause', () => new Error('w', { cause: 'socket hang up' })], |
| 105 | + ['an object cause', () => new Error('w', { cause: { code: 'ECONNRESET' } })], |
| 106 | + ]; |
| 107 | + it.each(fixtures)('%s', (_name, make) => { |
| 108 | + const error = make(); |
| 109 | + const record = recordOf((logger) => logger.error('parity', error)); |
| 110 | + expect(record.error?.causes).toEqual(faultCauseChain(error)); |
| 111 | + }); |
| 112 | +}); |
0 commit comments