Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,27 @@ name: CI

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

node-version: "20.x"
registry-url: "https://registry.npmjs.org"
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Type check
run: npx tsc --noEmit

- name: Run tests
run: npm run test:run

- name: Build
run: npm run build
run: npm run test
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
- run: npm ci
- run: npm run test
- run: npm run build
- run: npm run test:run
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@ The `Try` class provides a fluent interface for handling async operations with a
### Basic Usage

```typescript
import Try from '@power-rent/try-catch';
import Try from '@power-rent/try-catch/nextjs';

// Execute and get result (throws on error)
const result = await new Try(asyncFunction, arg1, arg2).unwrap();

// Execute with default value (never throws)
const result = await new Try(asyncFunction, arg1, arg2).default('fallback');
const result = await new Try(asyncFunction, arg1, arg2)
.default('fallback')
.value();

// Execute and get error (returns Error or undefined)
const error = await new Try(asyncFunction, arg1, arg2).error();

// Re-throw errors after reporting to Sentry
// Report to Sentry and let the error bubble up
try {
const result = await new Try(asyncFunction, arg1, arg2)
.report('Failed to execute business logic')
.rethrow()
.unwrap();
} catch (error) {
// Handle the re-thrown error
// Handle the error
}
```

Expand All @@ -46,7 +47,8 @@ const result = await new Try(processUser, { id: 123, name: 'John' })
.report('Failed to process user') // Custom error message
.tag('operation', 'user-processing') // Add Sentry tag
.tag('priority', 'high') // Add another tag
.default(null);
.default(null)
.value();

// Check for errors without throwing
const error = await new Try(riskyOperation, data)
Expand Down Expand Up @@ -84,16 +86,16 @@ Record breadcrumbs for the provided parameter keys. Only works when the first ar
#### `.tag(name: string, value: string): Try<T, TArgs>`
Add a tag for Sentry error reporting. Can be called multiple times to add multiple tags.

#### `.rethrow(): Try<T, TArgs>`
Configure to re-throw the exception after reporting to Sentry. Use with `.unwrap()`.

### Execution Methods

#### `.unwrap(): Promise<Awaited<T>>`
Execute the function and return the result. Throws the original error if one occurred.

#### `.default<Return>(defaultValue: Return): Promise<Awaited<T> | Return>`
Execute and return a default value when an exception occurs. Never throws.
#### `.default<Return>(defaultValue: Return): Try<T, TArgs>`
Set a default value that will be returned by `.value()` when an exception occurs.

#### `.value(): Promise<Awaited<T> | Return | undefined>`
Execute the function and return the result, the configured default value, or `undefined` if an error occurs.

#### `.error(): Promise<Error | undefined>`
Execute the function and return the error if one occurred, or `undefined` if successful.
Expand All @@ -107,7 +109,8 @@ Execute the function and return the error if one occurred, or `undefined` if suc
const user = await new Try(fetchUser, userId)
.report('Failed to fetch user')
.breadcrumbs(['userId'])
.default(null);
.default(null)
.value();

// Pattern 2: Check errors explicitly
const error = await new Try(updateDatabase, data)
Expand All @@ -125,7 +128,6 @@ try {
const result = await new Try(criticalOperation, params)
.report('Critical operation failed')
.tag('critical', 'true')
.rethrow()
.unwrap();
} catch (error) {
// Handle critical failure
Expand All @@ -141,19 +143,18 @@ const result = await new Try(complexOperation, data)
.tag('version', '2.0')
.breadcrumbs(['transactionId', 'amount'])
.report('Payment processing failed')
.default({ success: false });
.default({ success: false })
.value();
```

## Features

- 🔗 **Fluent API** - Method chaining with immutable instances
- 🚀 **Promise-like interface** - Can be awaited directly
- 🔍 **Automatic Sentry integration** - Errors are automatically reported
- 🍞 **Breadcrumb support** - Add context to error reports
- 🏷️ **Tag support** - Add custom tags to Sentry reports
- 🎯 **TypeScript support** - Full type safety
- 🔄 **Flexible error handling** - Choose to ignore, use defaults, or re-throw
- ⚡ **Declarative** - No side effects, immutable configuration
- 🔄 **Flexible error handling** - Choose to ignore, use defaults, inspect errors, or let them bubble up

## Requirements

Expand Down
Loading