> ## Documentation Index
> Fetch the complete documentation index at: https://docs.useflytrap.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript SDK Reference

> Learn how you can reproduce bugs on your local development environment

### `capture`

```typescript theme={null}
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:

```typescript theme={null}
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`

```typescript theme={null}
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:

```typescript theme={null}
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`

```typescript theme={null}
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:

```typescript theme={null}
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`

```typescript theme={null}
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:

```typescript theme={null}
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`

```typescript theme={null}
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:

```typescript theme={null}
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.
}
```
