MCPcopy Create free account
hub / github.com/middleapi/orpc / safe

Function safe

packages/client/src/utils.ts:20–43  ·  view source on GitHub ↗
(promise: ClientPromiseResult<TOutput, TError>)

Source from the content-addressed store, hash-verified

18 * @see {@link https://orpc.dev/docs/client/error-handling Client Error Handling Docs}
19 */
20export async function safe<TOutput, TError = ThrowableError>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>> {
21 try {
22 const output = await promise
23 return Object.assign(
24 [null, output, false, true] satisfies [null, TOutput, false, true],
25 { error: null, data: output, isDefined: false as const, isSuccess: true as const },
26 )
27 }
28 catch (e) {
29 const error = e as TError
30
31 if (isDefinedError(error)) {
32 return Object.assign(
33 [error, undefined, true, false] satisfies [typeof error, undefined, true, false],
34 { error, data: undefined, isDefined: true as const, isSuccess: false as const },
35 )
36 }
37
38 return Object.assign(
39 [error as Exclude<TError, ORPCError<any, any>>, undefined, false, false] satisfies [Exclude<TError, ORPCError<any, any>>, undefined, false, false],
40 { error: error as Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false as const, isSuccess: false as const },
41 )
42 }
43}
44
45export function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T> {
46 return {

Callers 15

createSafeClientFunction · 0.90
utils.test-d.tsFile · 0.90
utils.test.tsFile · 0.90
e2e.test-d.tsFile · 0.90
e2e.test.tsFile · 0.90
useServerActionFunction · 0.90

Calls 1

isDefinedErrorFunction · 0.90

Tested by

no test coverage detected