tryError Documentation

Function: chainError()
Function

```ts function chainError<T, E>( originalError, newType, newMessage, additionalFields? ): E; ``` Defined in: [factories.ts:385](https://github.com/oconnorjohnson/try-error/blob/e3ae0308069a4fba073f4543d527ad76373db795/src/factories.ts#L385) Chain errors while preserving the original error context Useful for wrapping lower-level errors with higher-level context while maintaining the full error chain for debugging.

tryError API Documentation v0.0.1-alpha.1


tryError API Documentation / chainError

Function: chainError()

function chainError<T, E>(
  originalError,
  newType,
  newMessage,
  additionalFields?
): E;

Defined in: factories.ts:385

Chain errors while preserving the original error context

Useful for wrapping lower-level errors with higher-level context while maintaining the full error chain for debugging.

Type Parameters

T

T extends string

E

E extends TryError<T>

Parameters

originalError

TryError

The original error to chain from

newType

T

The type for the new error

newMessage

string

The message for the new error

additionalFields?

Partial<Omit<E, keyof TryError<string>>>

Additional domain-specific fields

Returns

E

A new error with the original error as the cause

Example

const dbError = trySync(() => database.query("SELECT * FROM users"));
if (isErr(dbError)) {
  return chainError(dbError, "UserServiceError", "Failed to fetch user data", {
    operation: "getUserById",
    userId: "123",
  });
}