Skip to content

Commit a72b4e3

Browse files
committed
docs: update README
1 parent 7299544 commit a72b4e3

File tree

3 files changed

+63
-55
lines changed

3 files changed

+63
-55
lines changed

README.md

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,70 @@
1-
<p>
2-
<img width="100%" src="https://assets.solidjs.com/banner?type=Ecosystem&background=tiles&project=library-name" alt="solid-create-script">
3-
</p>
1+
# @dschz/try-catch
42

5-
# Template: SolidJS Library
3+
> A tiny utility to wrap promises or async functions and return a `[error, data]` tuple — no more `try/catch` boilerplate.
64
7-
Template for [SolidJS](https://www.solidjs.com/) library package. Bundling of the library is managed by [tsup](https://tsup.egoist.dev/).
5+
## ✨ Features
86

9-
Other things configured include:
7+
- ✅ Supports both async functions and raw promises
8+
- ✅ Catches both **sync and async** errors
9+
- ✅ Strongly typed result via `Result<T, E>`
10+
- ✅ Zero dependencies — just TypeScript
1011

11-
- Bun (for dependency management and running scripts)
12-
- TypeScript
13-
- ESLint / Prettier
14-
- Solid Testing Library + Vitest (for testing)
15-
- Playground app using library
16-
- GitHub Actions (for all CI/CD)
12+
## 📆 Installation
1713

18-
## Getting Started
14+
```bash
15+
npm install @dschz/try-catch
16+
pnpm install @dschz/try-catch
17+
yarn install @dschz/try-catch
18+
bun install @dschz/try-catch
19+
```
1920

20-
Some pre-requisites before install dependencies:
21+
## 🚀 Usage
2122

22-
- Install Node Version Manager (NVM)
23-
```bash
24-
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
25-
```
26-
- Install Bun
27-
```bash
28-
curl -fsSL https://bun.sh/install | bash
29-
```
23+
```ts
24+
import { tryCatch } from "@dschz/try-catch";
3025

31-
### Installing Dependencies
26+
const [err, data] = await tryCatch(fetch("/api/data"));
3227

33-
```bash
34-
nvm use
35-
bun install
28+
if (err) {
29+
console.error("Something went wrong:", err);
30+
} else {
31+
console.log("Data:", data);
32+
}
3633
```
3734

38-
### Local Development Build
35+
You can also pass a function that returns a promise:
3936

40-
```bash
41-
bun start
37+
```ts
38+
const [err, user] = await tryCatch(() => fetchUserById(123));
4239
```
4340

44-
### Linting & Formatting
41+
## 🧠 Types
4542

46-
```bash
47-
bun run lint # checks source for lint violations
48-
bun run format # checks source for format violations
43+
```ts
44+
type Success<T> = [error: null, data: T];
45+
type Failure<E extends Error = Error> = [error: E, data: null];
46+
type Result<T, E extends Error = Error> = Success<T> | Failure<E>;
47+
```
48+
49+
The return value is a tuple:
4950

50-
bun run lint:fix # fixes lint violations
51-
bun run format:fix # fixes format violations
51+
```ts
52+
[error, data]; // One will always be null
5253
```
5354

54-
### Contributing
55+
## 🧪 Example with Custom Error Types
56+
57+
```ts
58+
class MyError extends Error {
59+
constructor(message: string) {
60+
super(message);
61+
this.name = "MyError";
62+
}
63+
}
64+
65+
const [err, data] = await tryCatch<MyType, MyError>(() => doSomething());
66+
```
5567

56-
The only requirements when contributing are:
68+
## 📄 License
5769

58-
- You keep a clean git history in your branch
59-
- rebasing `main` instead of making merge commits.
60-
- Using proper commit message formats that adhere to [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/)
61-
- Additionally, squashing (via rebase) commits that are not [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/)
62-
- CI checks pass before merging into `main`
70+
MIT © [@dschz](https://github.com/thedanchez)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"lint:fix": "eslint . --fix",
3838
"pkg:changeset": "changeset",
3939
"pkg:version": "changeset version",
40-
"pkg:release": "changeset publish",
40+
"pkg:publish": "changeset publish",
4141
"serve": "vite preview",
4242
"start": "vite",
4343
"test": "vitest run",

src/__tests__/tryCatch.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@ describe("UTIL FUNCTION: tryCatch", () => {
99
expect(data).toBe(42);
1010
});
1111

12-
it('handles rejected promises correctly', async () => {
13-
const error = new Error('Oops!');
12+
it("handles rejected promises correctly", async () => {
13+
const error = new Error("Oops!");
1414
const [err, data] = await tryCatch(Promise.reject(error));
1515
expect(err).toBeInstanceOf(Error);
16-
expect(err?.message).toBe('Oops!');
16+
expect(err?.message).toBe("Oops!");
1717
expect(data).toBeNull();
1818
});
1919

20-
it('handles successful async functions', async () => {
21-
const [err, data] = await tryCatch(async () => 'done');
20+
it("handles successful async functions", async () => {
21+
const [err, data] = await tryCatch(async () => "done");
2222
expect(err).toBeNull();
23-
expect(data).toBe('done');
23+
expect(data).toBe("done");
2424
});
2525

26-
it('handles async functions that throw', async () => {
26+
it("handles async functions that throw", async () => {
2727
const [err, data] = await tryCatch(async () => {
28-
throw new TypeError('Bad input');
28+
throw new TypeError("Bad input");
2929
});
3030
expect(err).toBeInstanceOf(TypeError);
31-
expect(err?.message).toBe('Bad input');
31+
expect(err?.message).toBe("Bad input");
3232
expect(data).toBeNull();
3333
});
3434

35-
it('wraps non-Error throws into an Error', async () => {
35+
it("wraps non-Error throws into an Error", async () => {
3636
const [err, data] = await tryCatch(async () => {
37-
throw 'string error';
37+
throw "string error";
3838
});
3939
expect(err).toBeInstanceOf(Error);
40-
expect(err?.message).toBe('string error');
40+
expect(err?.message).toBe("string error");
4141
expect(data).toBeNull();
4242
});
4343
});

0 commit comments

Comments
 (0)