[SDK] Add lex client seam and typed xrpc error matching (#11348)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+13
-30
@@ -1,7 +1,6 @@
|
||||
import {createContext, useCallback, useContext} from 'react'
|
||||
import {LayoutAnimation} from 'react-native'
|
||||
import {type ComAtprotoServerDescribeServer} from '@atproto/api'
|
||||
import {XrpcResponseError} from '@atproto/lex'
|
||||
import {useLingui} from '@lingui/react/macro'
|
||||
import * as EmailValidator from 'email-validator'
|
||||
|
||||
@@ -9,9 +8,11 @@ import {DEFAULT_SERVICE} from '#/lib/constants'
|
||||
import {cleanError, isNetworkError} from '#/lib/strings/errors'
|
||||
import {createFullHandle} from '#/lib/strings/handles'
|
||||
import {getAge} from '#/lib/strings/time'
|
||||
import {matchXrpcError} from '#/lib/xrpc-error'
|
||||
import {useSessionApi} from '#/state/session'
|
||||
import {useOnboardingDispatch} from '#/state/shell'
|
||||
import {type AnalyticsContextType, useAnalytics} from '#/analytics'
|
||||
import {com} from '#/lexicons'
|
||||
|
||||
export type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema
|
||||
|
||||
@@ -258,34 +259,13 @@ export const useSignupContext = () => useContext(SignupContext)
|
||||
* failure is unexpected and should be reported to Sentry.
|
||||
*/
|
||||
function classifyExpectedSignupError(e: unknown): string | undefined {
|
||||
/*
|
||||
* TODO: `XrpcResponseError.error` is the open `LexErrorCode` union, so these
|
||||
* codes are compared as plain strings and a typo silently never matches.
|
||||
* Once the generated lexicons land, replace this (and every multi-code error
|
||||
* site) with a shared helper that narrows against the method schema:
|
||||
*
|
||||
* function matchXrpcError<M extends Procedure | Query>(
|
||||
* e: unknown,
|
||||
* method: Main<M>,
|
||||
* ): InferMethodError<M> | undefined
|
||||
*
|
||||
* switch (matchXrpcError(e, com.atproto.server.createAccount)) {
|
||||
* case 'InvalidHandle': ...
|
||||
* }
|
||||
*
|
||||
* The return type is the method's declared-errors union, so a typo'd case is
|
||||
* a compile error, and undeclared codes fall through to `undefined`. The
|
||||
* helper should also match `e.method.nsid` so a declared code from a
|
||||
* different call cannot match, mirroring the old per-method error classes.
|
||||
*/
|
||||
if (e instanceof XrpcResponseError) {
|
||||
switch (e.error) {
|
||||
case 'InvalidHandle':
|
||||
case 'HandleNotAvailable':
|
||||
case 'InvalidPassword':
|
||||
case 'UnsupportedDomain':
|
||||
return e.error
|
||||
}
|
||||
const code = matchXrpcError(e, com.atproto.server.createAccount)
|
||||
switch (code) {
|
||||
case 'InvalidHandle':
|
||||
case 'HandleNotAvailable':
|
||||
case 'InvalidPassword':
|
||||
case 'UnsupportedDomain':
|
||||
return code
|
||||
}
|
||||
/* the server sends no typed error for this case */
|
||||
if (String(e).includes('Email already taken')) return 'EmailTaken'
|
||||
@@ -376,7 +356,10 @@ export function useSubmitSignup() {
|
||||
} catch (err) {
|
||||
const e = err as Error
|
||||
let errMsg = e.toString()
|
||||
if (e instanceof XrpcResponseError && e.error === 'InvalidInviteCode') {
|
||||
if (
|
||||
matchXrpcError(e, com.atproto.server.createAccount) ===
|
||||
'InvalidInviteCode'
|
||||
) {
|
||||
dispatch({
|
||||
type: 'setError',
|
||||
value: l`Invite code not accepted. Check that you input it correctly and try again.`,
|
||||
|
||||
Reference in New Issue
Block a user