{"id":24527612,"url":"https://github.com/offlinehacker/errorjs","last_synced_at":"2026-04-16T14:02:54.794Z","repository":{"id":57226826,"uuid":"74924882","full_name":"offlinehacker/errorjs","owner":"offlinehacker","description":"Extendable javascript errors","archived":false,"fork":false,"pushed_at":"2017-10-10T07:36:07.000Z","size":226,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T04:46:19.698Z","etag":null,"topics":["error","errors","flowtype","javascript","typescript"],"latest_commit_sha":null,"homepage":"https://offlinehacker.github.io/errorjs/","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/offlinehacker.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}},"created_at":"2016-11-28T01:07:52.000Z","updated_at":"2020-02-18T23:14:25.000Z","dependencies_parsed_at":"2022-08-24T15:51:44.575Z","dependency_job_id":null,"html_url":"https://github.com/offlinehacker/errorjs","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offlinehacker%2Ferrorjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offlinehacker%2Ferrorjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offlinehacker%2Ferrorjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offlinehacker%2Ferrorjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/offlinehacker","download_url":"https://codeload.github.com/offlinehacker/errorjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243760345,"owners_count":20343626,"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":["error","errors","flowtype","javascript","typescript"],"created_at":"2025-01-22T06:19:42.610Z","updated_at":"2025-10-05T01:35:45.604Z","avatar_url":"https://github.com/offlinehacker.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ErrorJS\n\n[![NPM Version](https://badge.fury.io/js/errorjs.svg)](http://badge.fury.io/js/errorjs)\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![Greenkeeper badge](https://badges.greenkeeper.io/offlinehacker/errorjs.svg)](https://greenkeeper.io/)\n[![Travis](https://img.shields.io/travis/offlinehacker/errorjs.svg)](https://travis-ci.org/offlinehacker/errorjs)\n[![Coveralls](https://img.shields.io/coveralls/offlinehacker/errorjs.svg)](https://coveralls.io/github/offlinehacker/errorjs)\n[![Dev Dependencies](https://david-dm.org/offlinehacker/errorjs/dev-status.svg)](https://david-dm.org/offlinehacker/errorjs?type=dev)\n\nExtendable javascript errors\n\n## About\n\nAfter extending javascript `Error` class in every single project and copying\nallways different bad snippets from stackoverflow, i wanted to have a good\nerror base that i can reuse in different javascript/typescript projects.\nThis library tries to provide good simple base for javascript errors.\n\n## Install\n\n```\nnpm install --save errorjs\n```\n\n## Features\n\n### Sane predefined errors\n\nErrorJS has a list of predefined errors, that you can reuse while devloping a service.\n\n```javascript\nimport {\n  NotImplementedError,\n  InternalError,\n  NotFoundError,\n  UnauthorizedError,\n  ConnectionError,\n  ValidationError,\n  ConflictError,\n  LogicalError\n } from 'errorjs';\n\nthrow new NotImplementedError('some_feature_not_implemented');\nthrow new InternalError('something_internally');\nthrow new NotFoundError('something_was_not_found');\nthrow new UnauthorizedError('access_was_unauthroized');\nthrow new ConnectionError('error_with_connection_to_server');\nthrow new ValidationError('error_validating_resource');\nthrow new ConflictError('some_conflict');\nthrow new LogicalError('some_logic_error');\n```\n\n### Enforced error codes\n\nEvery error must have error code provided, which should uniquely identify an error\n\n```javascript\nimport {ConflictError} from 'errorjs';\n\nthrow new ConflictError(); // \u003c-- invalid since no error code is provided\nthrow new ConflictError('some_error_code'); // \u003c-- good, since error code is provided\n```\n\n### Support for error context\n\nAdditional context can be passed when creating an error\n\n**Simple error context**\n\n```javascript\nimport {ConflictError} from 'errorjs';\n\nthrow new ConflictError('some_error_code', {some: 'context'});\n```\n\n**Error class with predefined error context**\n\n```javascript\nconst ConflictWithContext = ConflictError.withContext({some: 'context'});\n\nthrow new ConflictWithContext('some_error_code', {some: 'othercontext'});\n```\n\n**Error factory with predefined error context**\n\n```javascript\nimport {errors} from 'errorjs';\n\nconst contextErrors = errors.withContext({userId: '\u003csome_user_id\u003e'});\n\nconst error = new contextErrors.ConflictError('user_exists', {errorId: '\u003csome_error_id\u003e'});\n\nerror.context // {userId: '\u003csome_user_id\u003e', errorId: '\u003csome_error_id\u003e'}\n```\n\n### Support for child errors\n\nChild errors can be passed to an error to encapsulate errors\n\n```javascript\nimport {ConflictError} from 'errorjs';\n\nconst baseError = new Error('some base error');\nconst baseError2 = new Error('some other error');\n\nthrow new ConflictError('some_error_code', baseError, baseError2);\n```\n\n### Variable arguments\n\nError constructor accepts variable number of arguments. This enable to\npass multiple context objects to error.\n\n```javascript\nimport {ValidationError} from 'errorjs';\n\nconst baseError = new Error('some error has happend');\n\nthrow new ValidationError('some_error_code', baseError, {some: 'context'}, {some: 'other_context'});\n```\n\n### TypeScript/flowtype support\n\nErrorJS is tightly integrated with typescript and provides type information.\n\n```javascript\nimport {errors} from 'errorjs';\n\nerrors.NotImplemented // \u003c--- autocomplete here\n\nnew errors.NotFoundError( /* autocomplete here */ );\n```\n\n### Extendable error factory and base error\n\nError factory can be extended with new errors classes and base error can be redefined.\n\n```javascript\nimport {ExtendedErrorFactory} from 'errorjs';\n\nclass MyVeryBadErrorFactory extends ExtendedErrorFactory {\n  BaseError = class BaseError extends this.BaseError {\n    isVeryBadError = true;\n\n    get isUserError() {\n      return this.context.userID;\n    }\n  };\n\n  MyError = this.defineErrorClass(\n    class MyError extends this.BaseError {\n      meta = {httpCode: 500};\n    }\n  );\n}\n\nconst factory = new MyErrorFactory();\nconst error = factory.NotFoundError('some_error', {userID: '123-123'});\n\nerror.isVeryBadError // \u003c-- extended functionality here\n\nthrow new factory.MyError('some_error_code');\n```\n\n## Examples\n\n### TypeScript\n\n```javascript\nimport {ExtendedErrorFactory} from 'errorjs';\n\nclass MyErrorFactory extends ExtendedErrorFactory {\n  BaseError = class MyBaseError extends this.BaseError {\n    scope = 'global';\n\n    get isUserError() {\n      return this.context.userId;\n    }\n  };\n\n  TransactionError = this.defineErrorClass(\n    class TransactionError extends this.ConflictError {\n      scope = 'transaction';\n\n      get userMessage() {\n        return `{${this.scope}} ${super.userMessage}`;\n      }\n    }\n  );\n}\n\nconst errorFactory = new MyErrorFactory();\n\nconst userErrors = factory.withContext({userId: '\u003csome_user_id\u003e'});\n\nthrow new userErrors.TransactionError('transaction_not_found', 'transaction was not found', {\n  id: '\u003ctransaction_id\u003e'\n});\n```\n\n### JavaScript\n\n```javascript\nconst {ExtendedErrorFactory} = require('errorjs');\n\nclass MyErrorFactory extends ExtendedErrorFactory {\n  constructor(...args) {\n    super(...args);\n\n    this.BaseError = class MyBaseError extends this.BaseError {\n      scope = 'global';\n\n      get isUserError() {\n        return this.context.userId;\n      }\n    };\n\n    this.TransactionError = this.defineErrorClass(\n      class TransactionError extends this.ConflictError {\n        get scope() {\n          return 'transaction';\n        }\n\n        get userMessage() {\n          return `{${this.scope}} ${super.userMessage}`;\n        }\n      }\n    );\n  }\n}\n\nconst errorFactory = new MyErrorFactory();\n\nconst userErrors = factory.withContext({userId: '\u003csome_user_id\u003e'});\n\nthrow new userErrors.TransactionError('transaction_not_found', 'transaction was not found', {\n  id: '\u003ctransaction_id\u003e'\n});\n```\n\n## Development\n\n - `npm t`: Run test suite\n - `npm start`: Runs `npm run build` in watch mode\n - `npm run test:watch`: Run test suite in [interactive watch mode](http://facebook.github.io/jest/docs/cli.html#watch)\n - `npm run test:prod`: Run linting and generate coverage\n - `npm run build`: Generage bundles and typings, create docs\n - `npm run lint`: Lints code\n - `npm run commit`: Commit using conventional commit style ([husky](https://github.com/typicode/husky) will tell you to use it if you haven't :wink:)\n\n## Credits\n\nMade with :heart: by [@offlinehacker](https://twitter.com/offlinehacker)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fofflinehacker%2Ferrorjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fofflinehacker%2Ferrorjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fofflinehacker%2Ferrorjs/lists"}