Skip to content

Commit 3692d11

Browse files
committed
♻️ move to nextjs and refactor
Signed-off-by: w01fgang <sumin@unix-center.ru>
1 parent 09ae76f commit 3692d11

7 files changed

Lines changed: 1293 additions & 322 deletions

File tree

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ The `Try` class provides a fluent interface for handling async operations with a
1515
### Basic Usage
1616

1717
```typescript
18-
import Try from '@power-rent/try-catch';
18+
import Try from '@power-rent/try-catch/nextjs';
1919

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

2323
// Execute with default value (never throws)
24-
const result = await new Try(asyncFunction, arg1, arg2).default('fallback');
24+
const result = await new Try(asyncFunction, arg1, arg2)
25+
.default('fallback')
26+
.value();
2527

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

29-
// Re-throw errors after reporting to Sentry
31+
// Report to Sentry and let the error bubble up
3032
try {
3133
const result = await new Try(asyncFunction, arg1, arg2)
3234
.report('Failed to execute business logic')
33-
.rethrow()
3435
.unwrap();
3536
} catch (error) {
36-
// Handle the re-thrown error
37+
// Handle the error
3738
}
3839
```
3940

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

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

87-
#### `.rethrow(): Try<T, TArgs>`
88-
Configure to re-throw the exception after reporting to Sentry. Use with `.unwrap()`.
89-
9089
### Execution Methods
9190

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

95-
#### `.default<Return>(defaultValue: Return): Promise<Awaited<T> | Return>`
96-
Execute and return a default value when an exception occurs. Never throws.
94+
#### `.default<Return>(defaultValue: Return): Try<T, TArgs>`
95+
Set a default value that will be returned by `.value()` when an exception occurs.
96+
97+
#### `.value(): Promise<Awaited<T> | Return | undefined>`
98+
Execute the function and return the result, the configured default value, or `undefined` if an error occurs.
9799

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

112115
// Pattern 2: Check errors explicitly
113116
const error = await new Try(updateDatabase, data)
@@ -125,7 +128,6 @@ try {
125128
const result = await new Try(criticalOperation, params)
126129
.report('Critical operation failed')
127130
.tag('critical', 'true')
128-
.rethrow()
129131
.unwrap();
130132
} catch (error) {
131133
// Handle critical failure
@@ -141,19 +143,18 @@ const result = await new Try(complexOperation, data)
141143
.tag('version', '2.0')
142144
.breadcrumbs(['transactionId', 'amount'])
143145
.report('Payment processing failed')
144-
.default({ success: false });
146+
.default({ success: false })
147+
.value();
145148
```
146149

147150
## Features
148151

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

158159
## Requirements
159160

0 commit comments

Comments
 (0)