capture

declare function capture<T extends Error>({ error, message }: {
    error: T;
    message?: string;
}): Promise<void>;

The capture function allows you to manually capture errors that you have captured. Useful when you want to continue executing the code, but want to send the bug to Flytrap.

Example usage:

import { capture } from 'useflytrap'
import { listUsers } from '@/lib/users'

function findUser(uid: string) {
	try {
		return listUsers().find((u) => u.id === id) ?? throw new Error(`User not found by ID ${uid}`)
	} catch (e) {
		capture({ error: e as Error, message: `findUser user not found by ID ${uid}` })
	}
}

tryCapture

declare function tryCapture<DType = unknown, EType = any>(fn: Promise<DType>): Promise<TryCatchResponse<DType, EType>>;

Allows you to call an async function that might throw, and automatically capture the error if it is thrown. Useful for when you want to manually handle error messages and such.

Example usage:

import { tryCapture } from 'useflytrap'
import { listUsers } from '@/lib/users'

function findUser(uid: string) {
	const { data: users, error } = await tryCapture(listUsers())
	if (!users || error) throw new Error()
	return users.find((u) => u.id === uid)
}

tryCaptureSync

declare function tryCaptureSync<DType = unknown, EType = any>(fn: () => DType): TryCatchResponse<DType, EType>;

Allows you to call a synchronous function that might throw, and automatically capture the error if it is thrown. Useful for when you want to manually handle error messages and such.

Example usage:

import { tryCaptureSync } from 'useflytrap'
import { listUsers } from '@/lib/users'


function findUser(uid: string) {
	const { data: user, error } = tryCaptureSync(() => listUsers().find((u) => u.id === uid))
	if (!user || error) throw new Error()
	return user
}

invariant

declare function invariant(condition: any, message?: string): asserts condition;

The invariant function is super good for type-safe invariant state checks. Because of the type-signature, TypeScript will be happy as well!

Example usage:

import { invariant } from 'useflytrap';
import { listUsers } from '@/lib/users';

function findUser(uid: string) {
	const user = listUsers().find((u) => u.id === uid)
	invariant(user, `Could not find user with ID ${uid}`)
	return user // 👈 here User is defined because of the invariant check
}

invariantAsync

declare function invariantAsync(condition: any, message?: string): Promise<void>;

The invariant function is super good for type-safe invariant state checks. The function will wait for the capture to be sent before throwing the invariant error.

Example usage:

import { invariantAsync } from 'useflytrap';
import { listUsers } from '@/lib/users';

async function findUser(uid: string) {
	const user = listUsers().find((u) => u.id === uid)
	await invariantAsync(user, `Could not find user with ID ${uid}`)
	return user! // 👈 Here we need to tell TypeScript explicitly that User is defined because `Promise` can't assert conditions.
}