Catching UnknownException and Converting to UnknownError in Effect Typescript

hi, is this the right way to catch such errors when
E
is unknown? here i want to catch UnknownException and convert it into UnknownError
export const RETRIEVAL_STRATEGY: unique symbol = Symbol('retrieval_strategy')

export type RetrievalStrategy<T extends LucidModel, K extends InstanceType<T>, A, E, R> = Brand.Branded<{
  query: ModelQueryBuilderContract<T, K>
  strategy: Effect.Effect<A | null | undefined, E | UnknownError, R>
}, typeof RETRIEVAL_STRATEGY>

export function makeRetrievalStrategy<T extends LucidModel, K extends InstanceType<T>, A extends InstanceType<T>, E, R>(
  query: ModelQueryBuilderContract<T, K>,
  strategy: (query: ModelQueryBuilderContract<T, K>) => Effect.Effect<A | null | undefined, E | Cause.UnknownException, R>,
) {
  return {
    query,
    strategy: strategy(query).pipe(
      Effect.catchIf(
        error => error instanceof Cause.UnknownException,
        error => ErrorUtility.toInternalUnknownError('Unknown error while retrieving the resource using the provided strategy.')(error.cause ?? error),
      ),
    ),
  } as RetrievalStrategy<T, K, A, Exclude<E, Cause.UnknownException> | UnknownError, R>
}


in addition to this, how can i conditionally type UnknownError only when UnknownException is there in
E
, right now,
E
always as
UnknownError
Was this page helpful?