import { WorkflowWorldError } from '@workflow/errors'; import { describe, expect, it } from 'vitest'; import { asTransportError, connectionLossOf, withTransportErrors, } from './transport-errors.js'; /** A driver error as `pg` raises it, optionally wrapped as Drizzle does. */ function pgError(message: string, code?: string) { return Object.assign(new Error(message), code ? { code } : {}); } function drizzleWrapped(cause: unknown) { return Object.assign( new Error( 'Failed query: insert into "workflow"."workflow_events" values ($1)\nparams: secret-payload' ), { cause } ); } describe('connectionLossOf', () => { it.each([ ['connection failure (08006)', pgError('connection failure', '08006')], [ 'admin shutdown (57P01)', pgError('terminating connection due to administrator command', '57P01'), ], [ 'a server starting up (57P03)', pgError('the database system is starting up', '57P03'), ], ['a reset socket', pgError('read ECONNRESET', 'ECONNRESET')], ['a refused socket', pgError('connect ECONNREFUSED', 'ECONNREFUSED')], ['a dropped client', pgError('Connection terminated unexpectedly')], [ 'a pool that could not connect', pgError('timeout exceeded when trying to connect'), ], [ 'a lost connection behind a Drizzle error', drizzleWrapped(pgError('Connection terminated unexpectedly')), ], ])('recognizes %s', (_label, error) => { expect(connectionLossOf(error)).toBeDefined(); }); it.each([ ['a serialization failure', pgError('could not serialize access', '40001')], ['a deadlock', pgError('deadlock detected', '40P01')], ['a unique violation', pgError('duplicate key value', '23505')], [ 'a client the World closed', pgError('Client was closed and is not queryable'), ], ['a plain error', new Error('nope')], ['a non-error', 'Connection terminated'], ])('does not take %s for connection loss', (_label, error) => { expect(connectionLossOf(error)).toBeUndefined(); }); }); describe('asTransportError', () => { it('maps a lost connection to a TRANSPORT World error, without the statement or the client', () => { const driver = Object.assign( pgError('terminating connection due to administrator command', '57P01'), { client: { connectionParameters: { password: 'hunter2' } } } ); const mapped = asTransportError(drizzleWrapped(driver)); expect(WorkflowWorldError.is(mapped)).toBe(true); expect(mapped).toMatchObject({ code: 'TRANSPORT' }); expect((mapped as Error).message).toBe( 'PostgreSQL connection lost (57P01): terminating connection due to administrator command' ); expect((mapped as Error).cause).toBeUndefined(); expect(JSON.stringify(mapped)).not.toContain('hunter2'); expect((mapped as Error).message).not.toContain('secret-payload'); }); it('leaves World errors and other errors as they are', () => { const world = new WorkflowWorldError('busy', { status: 503 }); const other = pgError('duplicate key value', '23505'); expect(asTransportError(world)).toBe(world); expect(asTransportError(other)).toBe(other); }); }); describe('withTransportErrors', () => { const lost = () => pgError('Connection terminated unexpectedly'); it('maps a rejection and a synchronous throw, and keeps `this`', async () => { const target = { factor: 2, async double(n: number) { return n * this.factor; }, async rejects(): Promise { throw lost(); }, throws(): never { throw lost(); }, }; const wrapped = withTransportErrors(target); await expect(wrapped.double(21)).resolves.toBe(42); await expect(wrapped.rejects()).rejects.toMatchObject({ name: 'WorkflowWorldError', code: 'TRANSPORT', }); expect(() => wrapped.throws()).toThrow( expect.objectContaining({ code: 'TRANSPORT' }) ); }); it('returns what a method that is not async returns, as it is', () => { const unsubscribe = () => {}; const wrapped = withTransportErrors({ subscribe: () => unsubscribe, label: 'events', }); expect(wrapped.subscribe()).toBe(unsubscribe); expect(wrapped.label).toBe('events'); }); });