https://github.com/gplopes/capsule
Capsule is a simple function wrapper that allows to handle errors more gracefully (WIP)
https://github.com/gplopes/capsule
Last synced: 8 months ago
JSON representation
Capsule is a simple function wrapper that allows to handle errors more gracefully (WIP)
- Host: GitHub
- URL: https://github.com/gplopes/capsule
- Owner: gplopes
- Created: 2024-05-05T21:21:38.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T09:29:19.000Z (about 2 years ago)
- Last Synced: 2025-02-01T16:22:03.386Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 109 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Capsule
## Description
Capsule is a lightweight function wrapper designed to enhance error handling in your codebase with grace and simplicity. With Capsule, managing errors becomes intuitive and flexible, allowing for cleaner and more robust code.
## Usage
By encapsulating any function with `Capsule` or `SyncCapsule`, you enable it to return another function where error handlers can be seamlessly integrated. Capsule offers multiple ways to catch and manage errors:
- `CatchError(ErrorInstance, callback)`: Utilize specific error instances for targeted handling.
- `CatchErrorBy([key, value], callback)`: Match errors based on key-value pairs, such as "[status, 404]".
- `UnCaughtError(callback)`: Define a fallback for any unforeseen errors.
Capsule operates non-invasively, meaning it won't propagate errors unless instructed to do so explicitly. If you wish to bubble up an error, simply re-throw it:
```typescript
UnCaughtError((error) => {
throw error;
});
```
Furthermore, Capsule optimizes error handling by employing a first-match approach. Once a match is found, it exits the check.
```tsx
const requestData = Capsule(async (id: string) => {
const response = await fetch(`.../${id}`);
const responseData = await response.json();
return responseData.data;
})(
UnCaughtError((error) => console.log("Unknown error:", error)),
CatchErrorBy(["status", 401], (error) => console.log("Unauthorized access:", error))
);
const result = await requestData("");
```