(promise: ClientPromiseResult<TOutput, TError>)
| 18 | * @see {@link https://orpc.dev/docs/client/error-handling Client Error Handling Docs} |
| 19 | */ |
| 20 | export 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 | |
| 45 | export function resolveFriendlyClientOptions<T extends ClientContext>(options: FriendlyClientOptions<T>): ClientOptions<T> { |
| 46 | return { |
no test coverage detected