Skip to main content

Overview

wrapClient creates a reusable wrapper around any Promise-based client (like API clients, database clients, etc.) that converts operations into Effects with consistent error handling.

Signature

Parameters

CLIENT
required
The client instance to wrap. Can be any object with Promise-returning methods.
(ctx: { cause: unknown; message?: string }) => ERROR
required
Factory function that creates your custom error type from a cause and optional message.

Returns

Returns a wrapper function that accepts:
(client: CLIENT) => Promise<OUTPUT>
required
A function that uses the client to perform an operation.
(cause: unknown) => OVERRIDEN_ERROR
Optional custom error handler for this specific operation. Overrides the default error factory.
string | (cause: unknown) => string
Optional error message (static string or function). Passed to the error factory.
The wrapper returns an Effect.Effect<OUTPUT, ERROR | OVERRIDEN_ERROR>.

Basic Usage

Simple Client Wrapper

Error Handling Options

Default Error

Without overrides, uses the error factory provided to wrapClient:

Static Error Message

Provide a static string message:

Dynamic Error Message

Compute the error message from the cause:

Custom Error Handler

Completely override the error handling for specific operations:

Real-World Examples

HTTP Client Wrapper

Database Client Wrapper

Third-Party API Client

Type Safety

Generic Client Types

The wrapper preserves full type information:

Error Type Overrides

When using errorHandler, the error type is updated:

See Also