match lex errors with instanceof, drop getErrorName

`getErrorName` erased the typed error classes by flattening everything to a
string code. Anchor on the classes instead: `LexAuthFactorError` for the login
2FA branch, `XrpcResponseError` for the signup error codes.

`@atproto/lex-client` stays a direct dependency because `XrpcResponseError` is
used at runtime for `instanceof`. It is already in the runtime graph via
`@atproto/lex-password-session`, so the direct declaration adds no bundle
weight; it makes the import legal under pnpm's strict layout and pins the
version so both packages share one `LexError` identity.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-07-31 20:38:33 +03:00
parent 1d279dc894
commit 21d67da9a9
4 changed files with 18 additions and 87 deletions
-54
View File
@@ -1,54 +0,0 @@
import {XrpcResponseError} from '@atproto/lex-client'
import {LexAuthFactorError} from '@atproto/lex-password-session'
import {describe, expect, it} from '@jest/globals'
import {getErrorName} from '../lex-error'
/**
* A stand-in for the method schema an `XrpcResponseError` is built against.
* The generated lexicons are not available yet, and `XrpcResponseError` only
* reads `method` back out for `matchesSchemaErrors()`, which these tests never
* call - so a minimal object is enough.
*/
const method = {
nsid: 'com.atproto.server.createSession',
type: 'procedure',
errors: ['AuthFactorTokenRequired'],
} as unknown as ConstructorParameters<typeof XrpcResponseError>[0]
/** An error as the lex client builds one from a JSON error response body. */
function xrpcResponseError(error: string, message: string) {
return new XrpcResponseError(method, new Response(null, {status: 400}), {
encoding: 'application/json',
body: {error, message},
})
}
describe('getErrorName', () => {
it('returns the lexicon error code of an XRPC response error', () => {
const e = xrpcResponseError('InvalidToken', 'Bad token scope')
expect(getErrorName(e)).toBe('InvalidToken')
})
it('returns AuthFactorTokenRequired for a LexAuthFactorError', () => {
/*
* The 2FA case: `PasswordSession.login` throws this, and it extends
* `LexError` WITHOUT being an `XrpcError` - the reason `getErrorName` is
* gated on `LexError`.
*/
const cause = xrpcResponseError(
'AuthFactorTokenRequired',
'A sign in code has been sent to your email address',
)
expect(getErrorName(new LexAuthFactorError(cause))).toBe(
'AuthFactorTokenRequired',
)
})
it('returns undefined for non-lex errors', () => {
expect(getErrorName(new Error('InvalidToken'))).toBeUndefined()
expect(getErrorName('InvalidToken')).toBeUndefined()
expect(getErrorName(null)).toBeUndefined()
expect(getErrorName(undefined)).toBeUndefined()
})
})
-13
View File
@@ -1,13 +0,0 @@
import {LexError} from '@atproto/lex-client'
/**
* The lexicon error code (`err.error`). Gated on `LexError` (the base of the
* lex error hierarchy) rather than `XrpcError` so sibling `LexError` subclasses
* that are NOT `XrpcError` also surface their `.error` - notably
* `LexAuthFactorError` (`'AuthFactorTokenRequired'`), which `PasswordSession`
* throws for email-2FA logins. Every `XrpcError` is a `LexError`, so gating on
* the base covers server error responses too.
*/
export function getErrorName(e: unknown): string | undefined {
return e instanceof LexError ? e.error : undefined
}