Effect CommunityEC
Effect Community3y ago
23 replies
imagio

Preferred Pattern for Passing and Enhancing Errors without Nesting

When dealing with errors what's the preferred pattern for passing along and enhancing errors without adding nesting? For example

export interface ErrorCommon {
    report?: boolean
    error?: unknown
    notifyUser?: boolean
    message?: string
    userFriendlyMessage?: string
}

export class DatabaseConnectionError extends Data.TaggedClass("DatabaseConnectionError")<ErrorCommon> {}

const setupDatabaseConnection = () => pipe(
   //.... fetch auth data, tokens, setup connection, stuff that could fail with many different errors
   Effect.catchAllCause(c =>
      pipe(
        Effect.logError(`Error setting up database connection`, c),
        Effect.flatMap(() =>
          Effect.fail(new DatabaseConnectionError({message: `Error setting up database connection`, report: true, error: c})),
        ),
     ),
  ),
)


This ends up wrapping the error in another error. Nesting errors/causes like this probably isn't the best pattern. What's the preferred pattern to handle these situations?
Was this page helpful?