|
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 |
4 | 2 |
|
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. |
6 | 4 |
|
7 | | -Template for [SolidJS](https://www.solidjs.com/) library package. Bundling of the library is managed by [tsup](https://tsup.egoist.dev/). |
| 5 | +## ✨ Features |
8 | 6 |
|
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 |
10 | 11 |
|
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 |
17 | 13 |
|
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 | +``` |
19 | 20 |
|
20 | | -Some pre-requisites before install dependencies: |
| 21 | +## 🚀 Usage |
21 | 22 |
|
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"; |
30 | 25 |
|
31 | | -### Installing Dependencies |
| 26 | +const [err, data] = await tryCatch(fetch("/api/data")); |
32 | 27 |
|
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 | +} |
36 | 33 | ``` |
37 | 34 |
|
38 | | -### Local Development Build |
| 35 | +You can also pass a function that returns a promise: |
39 | 36 |
|
40 | | -```bash |
41 | | -bun start |
| 37 | +```ts |
| 38 | +const [err, user] = await tryCatch(() => fetchUserById(123)); |
42 | 39 | ``` |
43 | 40 |
|
44 | | -### Linting & Formatting |
| 41 | +## 🧠 Types |
45 | 42 |
|
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: |
49 | 50 |
|
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 |
52 | 53 | ``` |
53 | 54 |
|
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 | +``` |
55 | 67 |
|
56 | | -The only requirements when contributing are: |
| 68 | +## 📄 License |
57 | 69 |
|
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) |
0 commit comments