diff --git a/packages/rate-limit/src/errors.ts b/packages/rate-limit/src/errors.ts new file mode 100644 index 0000000..366d8d2 --- /dev/null +++ b/packages/rate-limit/src/errors.ts @@ -0,0 +1,33 @@ +/** + * Core's error brand, as the public protocol rather than as an import. + * + * `Symbol.for` is registry-wide, so this resolves to the same symbol core registered, in this copy + * of the process and in any other. That is what lets a plugin throw something core renders without + * depending on core at runtime — and it survives two copies of core in one tree, which `instanceof` + * does not, since each copy has its own class identity. + * + * The string is the contract (design spec 1.2). `unique symbol` is what TypeScript needs for a + * computed class field, and a `const` annotated with it satisfies that. + */ +const HTTP_ERROR: unique symbol = Symbol.for('green-tea.http-error'); + +/** + * Thrown when a caller exceeds a rule. Carries `Retry-After` so a client can back off rather than + * retry immediately and spend the next window too. + * + * Extends `Error`, not core's `HttpError`: core reads `status`, `message`, `body` and `headers` off + * whatever carries the brand, and never uses `instanceof`. + */ +export class TooManyRequests extends Error { + readonly [HTTP_ERROR] = true; + readonly status = 429; + readonly body: { message: string; status: number }; + readonly headers: Record; + + constructor(message: string, retryAfterSeconds: number) { + super(message); + this.name = 'TooManyRequests'; + this.body = { message, status: 429 }; + this.headers = { 'retry-after': String(retryAfterSeconds) }; + } +} diff --git a/packages/rate-limit/test/rate-limit.test.ts b/packages/rate-limit/test/rate-limit.test.ts index 81465b5..ac43e5e 100644 --- a/packages/rate-limit/test/rate-limit.test.ts +++ b/packages/rate-limit/test/rate-limit.test.ts @@ -1,6 +1,7 @@ import { strict as assert } from 'node:assert'; import { test } from 'node:test'; +import { TooManyRequests } from '../src/errors.ts'; import { MemoryStore } from '../src/memory-store.ts'; const sleep = (ms: number): Promise => new Promise((resolve) => setTimeout(resolve, ms)); @@ -66,3 +67,18 @@ test('close is idempotent, because close() is called more than once in real apps assert.doesNotThrow(() => store.close()); }); + +// Convention rule 8, checked where it bites. The class never extends core's HttpError — it carries a +// registry-wide symbol, which is what lets a plugin throw a 429 that core renders without importing +// core, and what keeps working when two copies of core are in one tree (`instanceof` does not). +test('carries the brand core recognises, and the fields core reads off it', () => { + const error = new TooManyRequests('slow down', 30); + + assert.equal((error as unknown as Record)[Symbol.for('green-tea.http-error')], true); + assert.equal(error.status, 429); + assert.equal(error.message, 'slow down'); + assert.equal(error.headers['retry-after'], '30'); + assert.deepEqual(error.body, { message: 'slow down', status: 429 }); + // Still an Error: a throw that is not one loses the stack, and core rethrows before rendering. + assert.ok(error instanceof Error); +});