{"id":17348824,"url":"https://github.com/gplopes/capsule","last_synced_at":"2025-10-14T22:07:07.549Z","repository":{"id":238656518,"uuid":"796413704","full_name":"gplopes/capsule","owner":"gplopes","description":"Capsule is a simple function wrapper that allows to handle errors more gracefully (WIP)","archived":false,"fork":false,"pushed_at":"2024-05-07T09:29:19.000Z","size":112,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T16:22:03.386Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gplopes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-05T21:21:38.000Z","updated_at":"2024-05-07T09:29:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"a358a311-6026-4132-8407-b553ef1ccf17","html_url":"https://github.com/gplopes/capsule","commit_stats":null,"previous_names":["gplopes/capsule"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gplopes%2Fcapsule","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gplopes%2Fcapsule/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gplopes%2Fcapsule/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gplopes%2Fcapsule/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gplopes","download_url":"https://codeload.github.com/gplopes/capsule/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245841644,"owners_count":20681183,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T16:53:39.627Z","updated_at":"2025-10-14T22:07:02.527Z","avatar_url":"https://github.com/gplopes.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Capsule\n\n## Description\n\nCapsule 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.\n\n## Usage\n\nBy 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:\n\n- `CatchError(ErrorInstance, callback)`: Utilize specific error instances for targeted handling.\n- `CatchErrorBy([key, value], callback)`: Match errors based on key-value pairs, such as \"[status, 404]\".\n- `UnCaughtError(callback)`: Define a fallback for any unforeseen errors.\n\nCapsule 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:\n\n```typescript\nUnCaughtError((error) =\u003e {\n    throw error;\n});\n```\n\nFurthermore, Capsule optimizes error handling by employing a first-match approach. Once a match is found, it exits the check.\n\n```tsx\nconst requestData = Capsule(async (id: string) =\u003e {\n    const response = await fetch(`.../${id}`);\n    const responseData = await response.json();\n    return responseData.data;\n})(\nUnCaughtError((error) =\u003e console.log(\"Unknown error:\", error)),\nCatchErrorBy([\"status\", 401], (error) =\u003e console.log(\"Unauthorized access:\", error))\n);\n\nconst result = await requestData(\"\u003cid\u003e\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgplopes%2Fcapsule","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgplopes%2Fcapsule","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgplopes%2Fcapsule/lists"}