Handling Nested Errors with Type-Safe API Interactions

What's the best way to handle nested errors? My particular use case is (type-safely) interacting with a remote API that only sometimes responds with errors in a specific schema.
const findRecord = Effect.fnUntraced(function*(id: number) {
  return Effect.tryPromise({
    try: () => Database.query(id),
    catch: (error) =>
      Schema.decodeUnknown(RecordNotFoundSchema)(error).pipe(Effect.orElseSucceed(() => RecordNotFoundFallback))
  })
})

Here RecordNotFoundSchema is a schema mocking the error response from the API. I believe
catchAll
might be a way around but I'm not sure if it's the most idiomatic method.
Was this page helpful?