apply tweaked version of matthieu's suggestion
This commit is contained in:
+16
-7
@@ -7,6 +7,18 @@ import {
|
|||||||
XrpcResponseError,
|
XrpcResponseError,
|
||||||
} from '@atproto/lex'
|
} from '@atproto/lex'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same nsid means `e` was thrown for this method schema, so `e` can be
|
||||||
|
* treated as an `XrpcResponseError<M>` - which is what lets the SDK's
|
||||||
|
* `matchesSchemaErrors()` narrow `e.error` to M's declared errors.
|
||||||
|
*/
|
||||||
|
function isThrownFor<M extends Procedure | Query>(
|
||||||
|
e: XrpcResponseError,
|
||||||
|
schema: M,
|
||||||
|
): e is XrpcResponseError<M> {
|
||||||
|
return e.method.nsid === schema.nsid
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The lexicon error code carried by `e`, narrowed to the errors DECLARED by
|
* The lexicon error code carried by `e`, narrowed to the errors DECLARED by
|
||||||
* `method`, or `undefined` when `e` is not such an error.
|
* `method`, or `undefined` when `e` is not such an error.
|
||||||
@@ -39,12 +51,9 @@ export function matchXrpcError<M extends Procedure | Query>(
|
|||||||
if (!(e instanceof XrpcResponseError)) {
|
if (!(e instanceof XrpcResponseError)) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
const schema: Procedure | Query = getMain(method)
|
const schema = getMain(method)
|
||||||
const thrownFor: Procedure | Query = e.method
|
if (isThrownFor(e, schema) && e.matchesSchemaErrors()) {
|
||||||
if (thrownFor.nsid !== schema.nsid) {
|
return e.error
|
||||||
return undefined
|
|
||||||
}
|
}
|
||||||
return schema.errors?.includes(e.error)
|
return undefined
|
||||||
? (e.error as InferMethodError<M>)
|
|
||||||
: undefined
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user