Skip to content

Commit 315de90

Browse files
authored
move nextjs & add CI configs (#9)
* ♻️ move to nextjs and refactor Signed-off-by: w01fgang <sumin@unix-center.ru> * Update GitHub Actions workflows for CI and publishing - Simplify CI workflow by removing matrix strategy for Node.js versions, now fixed to 20.x - Change npm install to npm ci for consistency in both CI and publish workflows - Update test command in CI workflow to run 'npm run test' instead of 'npm run test:run' Signed-off-by: w01fgang <sumin@unix-center.ru> --------- Signed-off-by: w01fgang <sumin@unix-center.ru>
1 parent 845ae1f commit 315de90

9 files changed

Lines changed: 1308 additions & 345 deletions

File tree

.github/workflows/ci.yml

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,27 @@ name: CI
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
test:
1111
runs-on: ubuntu-latest
12-
13-
strategy:
14-
matrix:
15-
node-version: [18.x, 20.x, 22.x]
1612

1713
steps:
1814
- uses: actions/checkout@v3
19-
20-
- name: Use Node.js ${{ matrix.node-version }}
21-
uses: actions/setup-node@v3
15+
- uses: actions/setup-node@v3
2216
with:
23-
node-version: ${{ matrix.node-version }}
24-
cache: 'npm'
25-
17+
node-version: "20.x"
18+
registry-url: "https://registry.npmjs.org"
19+
cache: "npm"
20+
2621
- name: Install dependencies
2722
run: npm ci
28-
23+
2924
- name: Type check
3025
run: npx tsc --noEmit
31-
26+
3227
- name: Run tests
33-
run: npm run test:run
34-
35-
- name: Build
36-
run: npm run build
28+
run: npm run test

.github/workflows/publish.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ jobs:
99
- uses: actions/checkout@v3
1010
- uses: actions/setup-node@v3
1111
with:
12-
node-version: '20.x'
13-
registry-url: 'https://registry.npmjs.org'
14-
- run: npm install
12+
node-version: "20.x"
13+
registry-url: "https://registry.npmjs.org"
14+
- run: npm ci
15+
- run: npm run test
1516
- run: npm run build
16-
- run: npm run test:run
1717
- run: npm publish
1818
env:
19-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
19+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

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)