errormail()

Wrap functions to automatically catch and report errors via email.

Curried Usage (Recommended)

import { errormail } from 'errormail';

async function fetchUserData(userId) {
  const response = await fetch(`/api/users/${userId}`);
  if (!response.ok) {
    throw new Error('Failed to fetch user');
  }
  return response.json();
}

// Call errormail(emailTo) to get a wrapper function
const safeFetchUserData = errormail('admin@example.com')(fetchUserData);

// Use it normally - errors are automatically reported
const user = await safeFetchUserData(123);

Direct Wrapping

const safeFn = errormail('admin@example.com', myFunction);

With Options

const safeFn = errormail(myFunction, {
  emailTo: 'admin@example.com',
  functionName: 'myFunction',
  context: { module: 'userService' },
  includeArgs: true,
  rethrow: true
});

Reusable Wrappers

// Create wrappers for specific emails
const wrapForAdmin = errormail('admin@example.com');
const wrapForDevOps = errormail('devops@example.com');

// Wrap multiple functions
const safeFn1 = wrapForAdmin(function1);
const safeFn2 = wrapForAdmin(function2);
const safeFn3 = wrapForDevOps(function3);

Options

emailTo

Email address to send error reports to.

Type: string | Optional

functionName

Name of the function for context in error reports.

Type: string | Optional

context

Additional context object to include in error reports.

Type: object | Optional

includeArgs

Whether to include function arguments in error reports.

Type: boolean | Default: true

rethrow

Whether to re-throw the error after reporting.

Type: boolean | Default: true