{"_id":"non-error","_rev":"4252307","name":"non-error","description":"An error subclass for wrapping non-error values","dist-tags":{"latest":"0.1.0"},"maintainers":[{"name":"sindresorhus","email":""}],"time":{"modified":"2026-03-31T23:56:43.000Z","created":"2025-10-18T10:21:54.141Z","0.1.0":"2025-10-18T10:21:54.141Z"},"users":{},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"repository":{"type":"git","url":"git+https://github.com/sindresorhus/non-error.git"},"versions":{"0.1.0":{"name":"non-error","version":"0.1.0","description":"An error subclass for wrapping non-error values","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/non-error.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"sideEffects":false,"engines":{"node":">=20"},"scripts":{"test":"xo && ava && tsd"},"keywords":["error","non-error","nonerror","throw","string","to","make","convert","wrap","value"],"devDependencies":{"ava":"^6.4.1","tsd":"^0.33.0","xo":"^1.2.2"},"gitHead":"ef9ce532fe74e99eac31b457e3ecb5fba03a2c4d","types":"./index.d.ts","_id":"non-error@0.1.0","bugs":{"url":"https://github.com/sindresorhus/non-error/issues"},"homepage":"https://github.com/sindresorhus/non-error#readme","_nodeVersion":"24.9.0","_npmVersion":"11.6.1","dist":{"shasum":"b78b7d9a67ccb03ac979f9758813336ca7521cf2","size":4473,"noattachment":false,"key":"/non-error/-/non-error-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/non-error/download/non-error-0.1.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/non-error_0.1.0_1760782913953_0.7941759008451625"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-10-18T10:21:54.141Z","publish_time":1760782914141,"_source_registry_name":"default","_cnpm_publish_time":1760782914141}},"readme":"# non-error\n\n> An error subclass for wrapping non-error values\n\nWrap non-error values in a proper `Error` subclass. JavaScript allows throwing any value, but only `Error` instances have stack traces and proper debugging context. This package converts non-error values (strings, numbers, objects, etc.) into proper errors while preserving the original value.\n\n## Install\n\n```sh\nnpm install non-error\n```\n\n## Usage\n\n```js\nimport NonError from 'non-error';\n\nconst error = new NonError('Something went wrong');\n\nconsole.log(error.message);\n//=> 'Non-error value: Something went wrong'\n\nconsole.log(error.value);\n//=> 'Something went wrong'\n\nconsole.log(error.isNonError);\n//=> true\n\n// Works with any value type\nnew NonError(404);\nnew NonError({code: 'ERR_NOT_FOUND'});\nnew NonError(undefined);\n```\n\n## API\n\n### `new NonError(value, options?)`\n\nWraps a non-error value into an `Error` object.\n\nThis class is meant to be used when a value that is not an `Error` needs to be thrown or used as an error. JavaScript allows throwing any value, but this is considered bad practice. This class helps enforce proper error handling by converting any thrown value into a proper `Error` instance.\n\n#### value\n\nType: `unknown`\n\nThe value to wrap.\n\nThe error message will be a string representation of this value, and the original value will be stored in the `value` property.\n\nIf value is already a `NonError` instance, it is returned as-is. If value is an `Error` instance, a `TypeError` is thrown (throw the Error directly instead).\n\n#### options\n\n##### superclass\n\nType: `ErrorConstructor`\\\nDefault: `Error`\n\nThe superclass to extend from instead of `Error`.\n\nThis can be useful if you need `NonError` to extend a custom error class.\n\n```js\nimport NonError from 'non-error';\n\nconst error = new NonError('test', {superclass: TypeError});\n\nconsole.log(error instanceof TypeError);\n//=> true\n\nconsole.log(error instanceof NonError);\n//=> true\n```\n\n### `error.isNonError`\n\nType: `true` <sup>(read-only)</sup>\n\nIdentify `NonError` instances. Always `true`.\n\n### `error.value`\n\nType: `unknown` <sup>(read-only)</sup>\n\nThe original unwrapped value.\n\n```js\nimport NonError from 'non-error';\n\nconst error = new NonError(404);\nconsole.log(error.value);\n//=> 404\n```\n\n### `NonError.isNonError(value)`\n\nReturns: `boolean`\n\nCheck if a value is a `NonError` instance.\n\n```js\nimport NonError from 'non-error';\n\nconst error = new NonError('test');\nconsole.log(NonError.isNonError(error));\n//=> true\n\nconsole.log(NonError.isNonError(new Error('test')));\n//=> false\n```\n\n### `NonError.try(callback)`\n\nExecutes the callback immediately and wraps any non-error throws in `NonError`. Real `Error` instances are re-thrown unchanged.\n\nSupports both sync and async functions.\n\n```js\nimport NonError from 'non-error';\n\n// Non-error throws get wrapped\ntry {\n\tNonError.try(() => {\n\t\tthrow 'string error';\n\t});\n} catch (error) {\n\tconsole.log(error.isNonError);\n\t//=> true\n}\n\n// Real errors pass through unchanged\ntry {\n\tNonError.try(() => {\n\t\tthrow new TypeError('type error');\n\t});\n} catch (error) {\n\tconsole.log(error instanceof TypeError);\n\t//=> true\n}\n```\n\n### `NonError.wrap(function)`\n\nReturns a wrapped function that catches non-error throws and wraps them in `NonError`. Real `Error` instances are re-thrown unchanged.\n\nSupports both sync and async functions.\n\nUseful for array methods, promise chains, and callbacks that are invoked synchronously or by code with error handling.\n\n```js\nimport NonError from 'non-error';\n\n// Array operations\nconst results = items.map(NonError.wrap(transform));\n```\n\n## Best practices\n\n- Always throw `Error` instances, never strings, numbers, or plain objects.\n- Use `NonError` to convert non-error values when needed.\n- Keep error cause chains proper (wrap non-error causes with `NonError`).\n- Better TypeScript ergonomics: by ensuring all thrown values are `Error` instances, error handling code can work with `Error` type instead of `unknown`.\n\n## FAQ\n\n### Why doesn't this accept the value in a `cause` option?\n\nYou might wonder why you can't do `new NonError('message', {cause: nonErrorValue})`.\n\nWhile JavaScript's spec allows any value in the `cause` property, I think this is a design mistake. The `cause` property should always be an `Error` instance (or `undefined`) because:\n- Non-error causes lack stack traces, losing critical debugging context about where the root cause originated\n- It breaks TypeScript ergonomics, forcing `error.cause` to be typed as `unknown` instead of `Error | undefined`\n- Every code path has to safe-guard against non-errors\n\n### How is this different from [`ensure-error`](https://github.com/sindresorhus/ensure-error)?\n\n`ensure-error` fixes everything: wraps non-errors AND normalizes broken `Error` instances (adds missing `stack`, recursively fixes `.cause` and `AggregateError#errors` chains). Use it in catch blocks when you want all errors normalized and cleaned up.\n\n`non-error` only wraps non-errors. Real `Error` instances pass through unchanged. It's a more low-level component.\n\n## Related\n\n- [ensure-error](https://github.com/sindresorhus/ensure-error) - Ensures a value is a valid error by making it one if not\n","_attachments":{},"homepage":"https://github.com/sindresorhus/non-error#readme","bugs":{"url":"https://github.com/sindresorhus/non-error/issues"},"license":"MIT"}