{"id":15117119,"url":"https://github.com/ehmicky/modern-errors-switch","last_synced_at":"2025-03-19T08:30:47.353Z","repository":{"id":63573048,"uuid":"568541705","full_name":"ehmicky/modern-errors-switch","owner":"ehmicky","description":"`modern-errors` plugin to execute class-specific logic.","archived":false,"fork":false,"pushed_at":"2025-03-13T05:04:08.000Z","size":5561,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-13T06:19:07.664Z","etag":null,"topics":["catch","class","classes","errors","inheritance","instanceof","javascript","library","mapping","message","modern-errors","modern-errors-plugin","nodejs","plugins","select","switch","switch-case","switch-case-statements","typescript","wrapper"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/ehmicky.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2022-11-20T21:01:20.000Z","updated_at":"2025-03-13T05:04:12.000Z","dependencies_parsed_at":"2024-08-23T23:34:48.655Z","dependency_job_id":"a93e92f7-d6ad-400c-ad11-8c0e898bf0ed","html_url":"https://github.com/ehmicky/modern-errors-switch","commit_stats":{"total_commits":163,"total_committers":3,"mean_commits":"54.333333333333336","dds":"0.018404907975460127","last_synced_commit":"4be85a8da79b9543e80164d5fd23ab55605d2907"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":"ehmicky/template-javascript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fmodern-errors-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fmodern-errors-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fmodern-errors-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fmodern-errors-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ehmicky","download_url":"https://codeload.github.com/ehmicky/modern-errors-switch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243976471,"owners_count":20377691,"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","class","classes","errors","inheritance","instanceof","javascript","library","mapping","message","modern-errors","modern-errors-plugin","nodejs","plugins","select","switch","switch-case","switch-case-statements","typescript","wrapper"],"created_at":"2024-09-26T01:45:50.661Z","updated_at":"2025-03-19T08:30:46.907Z","avatar_url":"https://github.com/ehmicky.png","language":"JavaScript","readme":"\u003cpicture\u003e\n  \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://raw.githubusercontent.com/ehmicky/design/main/modern-errors/modern-errors_dark.svg\"/\u003e\n  \u003cimg alt=\"modern-errors logo\" src=\"https://raw.githubusercontent.com/ehmicky/design/main/modern-errors/modern-errors.svg\" width=\"600\"/\u003e\n\u003c/picture\u003e\n\n[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js\u0026colorA=404040\u0026logoColor=66cc33)](https://www.npmjs.com/package/modern-errors-switch)\n[![Browsers](https://img.shields.io/badge/-Browsers-808080?logo=firefox\u0026colorA=404040)](https://unpkg.com/modern-errors-switch?module)\n[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript\u0026colorA=404040\u0026logoColor=0096ff)](/src/main.d.ts)\n[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov\u0026colorA=404040)](https://codecov.io/gh/ehmicky/modern-errors-switch)\n[![Minified size](https://img.shields.io/bundlephobia/minzip/modern-errors-switch?label\u0026colorA=404040\u0026colorB=808080\u0026logo=webpack)](https://bundlephobia.com/package/modern-errors-switch)\n[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon\u0026colorA=404040\u0026logoColor=9590F9)](https://fosstodon.org/@ehmicky)\n[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium\u0026colorA=404040)](https://medium.com/@ehmicky)\n\n[`modern-errors`](https://github.com/ehmicky/modern-errors)\n[plugin](https://github.com/ehmicky/modern-errors#-plugins) to execute\nclass-specific logic.\n\nThis adds [`BaseError.switch()`](#baseerrorswitcherror) to wrap an error\naccording to its class.\n\n# Hire me\n\nPlease\n[reach out](https://www.linkedin.com/feed/update/urn:li:activity:7117265228068716545/)\nif you're looking for a Node.js API or CLI engineer (11 years of experience).\nMost recently I have been [Netlify Build](https://github.com/netlify/build)'s\nand [Netlify Plugins](https://www.netlify.com/products/build/plugins/)'\ntechnical lead for 2.5 years. I am available for full-time remote positions.\n\n# Example\n\n[Adding the plugin](https://github.com/ehmicky/modern-errors#adding-plugins) to\n[`modern-errors`](https://github.com/ehmicky/modern-errors).\n\n```js\nimport ModernError from 'modern-errors'\n\nimport modernErrorsSwitch from 'modern-errors-switch'\n\nexport const BaseError = ModernError.subclass('BaseError', {\n  plugins: [modernErrorsSwitch],\n})\n// ...\n```\n\nWrapping an error's message according to its class.\n\n```js\ntry {\n  // ...\n} catch (error) {\n  throw BaseError.switch(error)\n    .case(InputError, 'The input was invalid.')\n    .case(DatabaseError, 'Bug at the database layer.')\n    .default('Unknown error.')\n}\n```\n\n# Install\n\n```bash\nnpm install modern-errors-switch\n```\n\nThis package works in both Node.js \u003e=18.18.0 and\n[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist).\n\nThis is an ES module. It must be loaded using\n[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),\nnot `require()`. If TypeScript is used, it must be configured to\n[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),\nnot CommonJS.\n\n# API\n\n## modernErrorsSwitch\n\n_Type_: `Plugin`\n\nPlugin object to pass to the\n[`plugins` option](https://github.com/ehmicky/modern-errors#adding-plugins) of\n`ErrorClass.subclass()`.\n\n## BaseError.switch(error)\n\n`error`: `unknown`\\\n_Return value_: [`Switch`](#switchcasecondition-effects)\n\nApply logic according to `error`'s class. This must be chained with\n[`.case()`](#switchcasecondition-effects) and end with\n[`.default()`](#switchdefaulteffects).\n\nAlthough `error` should be an `Error` instance most of the times, it can be of\nany type. However, the final value returned by\n[`.default()`](#switchdefaulteffects) is always an instance of `BaseError` or a\nsubclass of it.\n\n## Switch.case(condition, ...effects)\n\n`condition`: [`Condition`](#condition)\\\n`effect`: [`Effect`](#effect)\\\n_Return value_: [`Switch`](#switchcasecondition-effects)\n\nIf `error` matches the `condition`, apply the `effects`. 0, 1 or multiple\neffects can be applied.\n\n## Switch.default(...effects)\n\n`effect`: [`Effect`](#effect)\\\n_Return value_: `BaseError`\n\nIf none of the [`.case()`](#switchcasecondition-effects) statements matched,\napply those default `effects`.\n\n## Condition\n\nThe `condition` can be:\n\n- An error class, matched with\n  [`instanceof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof)\n- An error\n  [`name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name)\n  string\n- A filtering function taking the `error` as argument and returning a boolean\n\n## Effect\n\nEach `effect` can be:\n\n- A\n  [new error class](https://github.com/ehmicky/modern-errors#wrap-error-class).\n  It must be a\n  [subclass](https://github.com/ehmicky/modern-errors#create-error-classes) of\n  `BaseError`. It is ignored if `error`'s class is\n  [already a subclass](https://github.com/ehmicky/modern-errors#wrap-error-class).\n- A\n  [message](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message)\n  string to append or (if it ends with `:` or `:\\n`) prepend\n- An\n  [options object](https://github.com/ehmicky/modern-errors#wrap-error-options)\n  to merge\n- A mapping function taking the `error` as argument and returning it\n\n# Usage\n\n## Wrap error message\n\n```js\nBaseError.switch(error)\n  // If `InputError`, then appends the following message\n  .case(InputError, 'The input was invalid.')\n  // If the message ends with `:` or `:\\n`, it is prepended instead\n  .case(DatabaseError, 'Bug at the database layer:')\n  // Empty error messages are ignored\n  .default('')\n```\n\n## Convert error classes\n\n```js\n// Convert `TypeError` class to `InputError`, etc. or default to `UnknownError`\nBaseError.switch(error)\n  .case(TypeError, InputError)\n  .case(URIError, ClientError)\n  .default(UnknownError)\n```\n\n## Wrap error options\n\n```js\nBaseError.switch(error)\n  // If `DatabaseError`, add options for other plugins like `modern-errors-bugs`\n  .case(DatabaseError, { bugs: 'https://github.com/my-name/my-project/issues' })\n  .default()\n```\n\n## Map error\n\n\u003c!-- eslint-disable promise/prefer-await-to-callbacks --\u003e\n\n```js\nBaseError.switch(error)\n  // If `DatabaseError`, adds `error.databaseUrl`\n  .case(DatabaseError, (error) =\u003e {\n    error.databaseUrl = databaseUrl\n    return error\n  })\n  .default()\n```\n\n## Multiple effects\n\n```js\nBaseError.switch(error)\n  // 0 effects\n  .case(TypeError)\n  // Multiple effects\n  .case(URIError, ClientError, 'Invalid URI.', {\n    bugs: 'https://github.com/my-name/my-project/issues',\n  })\n  .default(UnknownError)\n```\n\n## Custom condition\n\n```js\nBaseError.switch(error)\n  // If `error.type` is `database`, append the following message\n  .case((error) =\u003e error.type === 'database', 'Bug at the database layer.')\n  .default()\n```\n\n## Unknown errors\n\n\u003c!-- eslint-disable unicorn/no-null, no-throw-literal,\n     promise/prefer-await-to-callbacks, no-shadow --\u003e\n\n```js\n// Any `error` can be wrapped, even if it is not an `Error` instance\ntry {\n  throw null\n} catch (error) {\n  // Therefore the filtering and mapping functions' argument might be anything\n  throw BaseError.switch(error)\n    .case(\n      (error) =\u003e error instanceof Error \u0026\u0026 error.type === 'database',\n      'Bug at the database layer.',\n    )\n    .default((error) =\u003e {\n      if (error instanceof Error) {\n        error.type = 'other'\n      }\n\n      return error\n    })\n}\n```\n\n# Related projects\n\n- [`log-process-errors`](https://github.com/ehmicky/log-process-errors): Show\n  some ❤ to Node.js process errors\n- [`modern-errors`](https://github.com/ehmicky/modern-errors): Handle errors in\n  a simple, stable, consistent way\n- [`modern-errors-cli`](https://github.com/ehmicky/modern-errors-cli): Handle\n  errors in CLI modules\n- [`modern-errors-process`](https://github.com/ehmicky/modern-errors-process):\n  Handle process errors\n- [`modern-errors-bugs`](https://github.com/ehmicky/modern-errors-bugs): Print\n  where to report bugs\n- [`modern-errors-serialize`](https://github.com/ehmicky/modern-errors-serialize):\n  Serialize/parse errors\n- [`modern-errors-clean`](https://github.com/ehmicky/modern-errors-clean): Clean\n  stack traces\n- [`modern-errors-http`](https://github.com/ehmicky/modern-errors-http): Create\n  HTTP error responses\n- [`modern-errors-winston`](https://github.com/ehmicky/modern-errors-winston):\n  Log errors with Winston\n\n# Support\n\nFor any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).\n\nEveryone is welcome regardless of personal background. We enforce a\n[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and\ninclusive environment.\n\n# Contributing\n\nThis project was made with ❤️. The simplest way to give back is by starring and\nsharing it online.\n\nIf the documentation is unclear or has a typo, please click on the page's `Edit`\nbutton (pencil icon) and suggest a correction.\n\nIf you would like to help us fix a bug or add a new feature, please check our\n[guidelines](CONTRIBUTING.md). Pull requests are welcome!\n\n\u003c!-- Thanks go to our wonderful contributors: --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START --\u003e\n\u003c!-- prettier-ignore --\u003e\n\u003c!--\n\u003ctable\u003e\u003ctr\u003e\u003ctd align=\"center\"\u003e\u003ca href=\"https://fosstodon.org/@ehmicky\"\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/8136211?v=4\" width=\"100px;\" alt=\"ehmicky\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eehmicky\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/ehmicky/modern-errors-switch/commits?author=ehmicky\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#design-ehmicky\" title=\"Design\"\u003e🎨\u003c/a\u003e \u003ca href=\"#ideas-ehmicky\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"https://github.com/ehmicky/modern-errors-switch/commits?author=ehmicky\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\n --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehmicky%2Fmodern-errors-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fehmicky%2Fmodern-errors-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehmicky%2Fmodern-errors-switch/lists"}