{"id":20046374,"url":"https://github.com/aneldev/dyna-error","last_synced_at":"2026-02-10T02:32:04.487Z","repository":{"id":87577660,"uuid":"326166851","full_name":"aneldev/dyna-error","owner":"aneldev","description":"Enrich Javascript's Errors","archived":false,"fork":false,"pushed_at":"2024-09-02T10:49:58.000Z","size":493,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-04T03:02:56.946Z","etag":null,"topics":[],"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/aneldev.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-02T11:20:43.000Z","updated_at":"2024-09-02T10:49:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"642d71e9-64aa-4a07-ae21-53123fda1b99","html_url":"https://github.com/aneldev/dyna-error","commit_stats":{"total_commits":54,"total_committers":4,"mean_commits":13.5,"dds":0.537037037037037,"last_synced_commit":"6022b84de89fb7867fe655795231ec882d07fab4"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"purl":"pkg:github/aneldev/dyna-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aneldev","download_url":"https://codeload.github.com/aneldev/dyna-error/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-error/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270957265,"owners_count":24675100,"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","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11-13T11:23:22.245Z","updated_at":"2026-02-10T02:32:03.547Z","avatar_url":"https://github.com/aneldev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dynaError\n\n`dynaError` enhances JavaScript's `Error` by adding more data beyond just a message.\n\nCreate more detailed errors with properties like:\n\n- `userMessage` for the end-user, or\n- Additional `data` for debugging\n\nAll this while keeping the benefits of the native `Error`, including the stack trace.\n\nWritten in TypeScript.\n\n# Usage\n_Examples are in TypeScript_\n\n## Import\n```typescript\nimport {\n  IDynaError,\n  dynaError,\n} from \"dyna-error\";\n```\n\n## Simple Example\n\nInstead of this:\n```typescript\nthrow new Error('Service not available');\n```\nYou can do this:\n```typescript\nthrow dynaError('Service not available');\n```\nor this:\n```typescript\nthrow dynaError({ message: 'Service not available' });\n```\nwhich is essentially the same.\n\nNow, let's add some more details to this error:\n\n```typescript\nthrow dynaError({\n  message: 'Service not available',\n  userMessage: 'Something went wrong, please retry',\n  canRetry: true,\n  data: {\n    serviceResponse: {...} // Additional info for debugging\n  }\n});\n```\n\n## Real Example\n\n```typescript\n// A fetch function\nconst getUserSalary = async (userId: string): Promise\u003cIUser\u003e =\u003e {\n  const salaryServiceAvailable = await fetchUserSalaryAvailable();\n  \n  if (!salaryServiceAvailable) throw dynaError({\n    message: 'Service not ready',\n    userMessage: 'System overloaded, please retry.',\n    canRetry: true,\n    data: {\n      salaryInfo,\n    },\n  });\n  \n  return fetchUserSalary();\n};\n\n\n// Catch the error\ntry {\n  await getUserSalary(userId);\n} catch (e) {\n  const error: IDynaError = e;  // You can safely cast it, even if e is not an IDynaError.\n  if (error.userMessage) alert(error.userMessage);\n  setState({ canRetry: !!error.canRetry });\n}\n```\n\n# API\n\n## dynaError Argument Object\n\n`dynaError` accepts a string as the error message **or** an object based on the `IErrorConfig` interface.\n\nIn `IErrorConfig`, only the `message` is required.\n\n```typescript\nexport interface IErrorConfig {\n  /**\n   * Error message intended for debugging purposes.\n   */\n  message: string;\n\n  /**\n   * User-friendly error message, ideally translated and devoid of sensitive information.\n   */\n  userMessage?: string;\n\n  /**\n   * Developer-assigned error code for identifying the point where the error occurred.\n   */\n  code?: number;\n\n  /**\n   * Network error status, which can be an HTTP code or any status understandable by other parts of the application.\n   */\n  status?: number;\n\n  /**\n   * Error data intended for debugging, may contain sensitive information.\n   */\n  data?: any;\n\n  /**\n   * Error data that can be safely delivered to the client or end-user.\n   */\n  userData?: any;\n\n  /**\n   * Reference to the parent error.\n   */\n  parentError?: any;\n\n  /**\n   * Validation errors associated with the error.\n   */\n  validationErrors?: any;\n\n  /**\n   * Stack trace representing the error.\n   *\n   * Collect stack or not.\n   * For security reasons (if the error is shipped to the client) might be not wanted.\n   *\n   * @default true\n   */\n  stack?: boolean;      // Do not collect stack (for security reasons)\n\n  /**\n   * Indicates whether the action that caused this error can be retried.\n   */\n  canRetry?: boolean;\n\n  /**\n   * If code is defined, the error message will be prefixed with the error code.\n   *\n   * @default false\n   */\n  prefixMessageWithCode?: boolean;\n\n  // For internal use, do not use it!.\n  _applyStackContent?: any;\n}\n```\n\nHere’s a full example of a `dynaError` being thrown:\n\n```typescript\nthrow dynaError({\n  message: 'Salary service not available',\n  userMessage: 'Please retry',\n  code: 330010,\n  status: 200,\n  parentError: e,\n  validationErrors: { loginName: 'Is required' },\n  canRetry: true,\n  data: {\n    userId: 230130042,\n    salaryServiceResponse,\n  },\n});\n```\n\n## dynaError Thrown Error\n\nThis is what `dynaError` returns:\n\n```typescript\nexport interface IDynaError extends Error {\n  date?: Date;\n  message: string;\n  userMessage?: string;\n  code?: number;\n  status?: number;\n  data?: any;\n  userData?: any;\n  parentError?: any;\n  stack?: string;\n  validationErrors?: any;\n  canRetry?: boolean;\n  isDynaError?: true;\n}\n```\n\nHere’s a full example of a `dynaError` being caught:\n\n```typescript\ntry {\n  return getSalary(userId);\n} catch(e) {\n  const error: IDynaError = e;\n  // Here you have all properties from the above IDynaError interface.\n  // You can safely cast e, even if it's not a dynaError.\n  // Since all properties of IDynaError are optional, the output cast is valid.\n}\n```\n\n# Summary\n\nIn JavaScript, you can throw anything as an error. It’s not wrong to throw an object as an error, but you miss a few things:\n\n- No `stack` trace\n- The error is not an `Error` instance\n\nWith `dynaError`, you get richer errors that are easier to handle.\n\n`IDynaError` is fully compatible with JavaScript’s `Error`.\n\n# Changelog\n\n## v1\n\n**First version**\n\n## v2\n\nExtended Native JS Error\n\n## v3\n\nReturns a new object compatible with JS Error.\n\nThis makes the error serializable with `JSON.stringify`.\n\n## v4\n\n- Compatible with `unknown` errors\n- Always collects a `stack` trace, but it can be disabled using the `stack` property.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faneldev%2Fdyna-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-error/lists"}