{"id":13847334,"url":"https://github.com/enkot/catch-decorator","last_synced_at":"2025-05-16T00:34:03.648Z","repository":{"id":57194982,"uuid":"144759661","full_name":"enkot/catch-decorator","owner":"enkot","description":"Allows you to handle exceptions in ECMAScript/Typescript classes with only one annotation","archived":false,"fork":false,"pushed_at":"2023-03-02T05:31:03.000Z","size":477,"stargazers_count":74,"open_issues_count":9,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-13T21:20:54.664Z","etag":null,"topics":["catch","decorator","errors","handling","javascript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enkot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-08-14T18:47:35.000Z","updated_at":"2025-04-25T02:16:32.000Z","dependencies_parsed_at":"2024-01-15T20:49:27.448Z","dependency_job_id":"2d3f7eb3-0276-4de1-a77a-f4a61eb20d41","html_url":"https://github.com/enkot/catch-decorator","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":0.2222222222222222,"last_synced_commit":"6258ac5e1e889f6c474fa1c9bfbf3a7f994d7696"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enkot%2Fcatch-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enkot%2Fcatch-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enkot%2Fcatch-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enkot%2Fcatch-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enkot","download_url":"https://codeload.github.com/enkot/catch-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254447925,"owners_count":22072755,"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":["catch","decorator","errors","handling","javascript"],"created_at":"2024-08-04T18:01:17.083Z","updated_at":"2025-05-16T00:33:58.625Z","avatar_url":"https://github.com/enkot.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# 🎣 catch-decorator\n[![Build Status](https://travis-ci.org/enkot/catch-decorator.svg?branch=master)](https://travis-ci.org/enkot/catch-decorator)\n\nAllows you to handle exceptions in class methods with only one annotation\n (decorator). Idea of errors handling taken from Java.\n\n \u003e *UPDATE* from v2: refactored to use stacked decorators style. Thx [@k1r0s](https://github.com/k1r0s) for idea :)  \n\n## Install\n\n```bash\nnpm install catch-decorator\n```\n\n## Why?\nThe main problem of handling errors are using \"try/catch\" blocks or \"catch\" methods for Promises. \nBut if we use classes, for example for Vue components, why can't we use method decorators for handling errors? \n\nSo, for example, instead of this:\n```js\nclass Messenger {\n    async getMessages() {\n        try\n            await api.getData() // \u003c-- can throw ServerError\n        } catch(err) {\n            ...\n        }   \n    }\n}\n```\nwe can write this:\n```js\nimport Catch from 'catch-decorator'\n\nclass Messenger {\n    @Catch(ServerError, handler)\n    async getMessages() {\n        await api.getData() // \u003c-- can throw custom ServerError\n    }\n}\n```\nmuch prettier, isn't it?\n\n\n## How to use?\n\u003e `catch-decorator` works with any ECMAScript/Typescript classes. If you use Babel, [babel-plugin-transform-decorators-legacy](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy) is needed. If you use TypeScript, enable `--experimentalDecorators` flag.\n\nYou can handle any thrown error:\n\n```js\nimport Catch from 'catch-decorator'\n\nconst CatchAll = Catch(Error, (err: any) =\u003e console.log(err.message))\n\nclass Messenger {\n    @CatchAll\n    getMessages() {\n        throw new TypeError('ReferenceError here!')\n        ...\n    }\n}\n```\n\nor write decorators in stack to handle more than one errors type. In callback as second argument will be passed current instance object (context):\n```js\nclass Messenger {\n    @Catch(TypeError, (err, ctx) =\u003e {...})\n    @Catch(ReferenceError, (err, ctx) =\u003e {...})\n    getMessages() {\n        throw new ReferenceError('ReferenceError here!')\n        ...\n    }\n}\n```\n\nIt also works with async methods:\n```js\nclass Messenger {\n    errorMessage = null\n\n    @Catch(ServerError, (err, ctx) =\u003e ctx.errorMessage = err.message)\n    getMessages() {\n        return fetch(myRequest).then(response =\u003e { // can throw ServerError\n            ...\n        })\n    }\n}\n```\n\n## License:\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenkot%2Fcatch-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenkot%2Fcatch-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenkot%2Fcatch-decorator/lists"}