{"_id":"@anandsuresh/smart-error","_rev":"2-8ddd9af76a26575948acebace8155307","name":"@anandsuresh/smart-error","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@anandsuresh/smart-error","version":"1.0.0","description":"A smart error implementation","license":"MIT","main":"index.js","repository":{"type":"git","url":"git+https://github.com/anandsuresh/smart-error.git"},"author":{"name":"Anand Suresh"},"bugs":{"url":"https://github.com/anandsuresh/smart-error/issues"},"homepage":"https://github.com/anandsuresh/smart-error#readme","engines":{"node":">=8.0.0"},"scripts":{"coverage":"NODE_ENV=test istanbul cover -i 'index.js' _mocha -- --ui bdd --reporter spec --recursive spec && open coverage/lcov-report/index.html > /dev/null 2>&1","lint":"standard","test":"mocha --ui bdd --reporter spec --recursive spec"},"devDependencies":{"chai":"4.1.2","istanbul":"1.1.0-alpha.1","mocha":"5.0.5","sinon":"5.0.0","standard":"11.0.1"},"standard":{"ignore":[],"globals":["after","afterEach","before","beforeEach","describe","it"]},"gitHead":"07c4a9fe9f9482d9ea09a2a4237f7561ef9bc3da","_id":"@anandsuresh/smart-error@1.0.0","_npmVersion":"5.8.0","_nodeVersion":"8.11.0","_npmUser":{"name":"anandsuresh","email":"anandsuresh@gmail.com"},"dist":{"integrity":"sha512-wVDbTMACWRBDO6sUM0ouGf65ImNYmzggQfzWCPg5dKtgEj5Y10ix9iDyz2cwjzw4Wo29LOsHzMbcu15Qt1F/zw==","shasum":"52edcb6f0acf4fa2eed5abd9148c08243e2c0ca5","tarball":"https://registry.npmjs.org/@anandsuresh/smart-error/-/smart-error-1.0.0.tgz","fileCount":6,"unpackedSize":12403,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCjOMqtoSTyCZ/HMv3mJP8ST6E5PO4N9oTfOadMfvVJFgIgdkrFAZqYOAbetu1u9MzmBHENcTOPi8hsJUkfVsaJG4o="}]},"maintainers":[{"name":"anandsuresh","email":"anandsuresh@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/smart-error_1.0.0_1523375479694_0.3050871573539349"},"_hasShrinkwrap":false}},"time":{"created":"2018-04-10T15:51:19.642Z","1.0.0":"2018-04-10T15:51:19.802Z","modified":"2022-04-04T13:55:15.446Z"},"maintainers":[{"name":"anandsuresh","email":"anandsuresh@gmail.com"}],"description":"A smart error implementation","homepage":"https://github.com/anandsuresh/smart-error#readme","repository":{"type":"git","url":"git+https://github.com/anandsuresh/smart-error.git"},"author":{"name":"Anand Suresh"},"bugs":{"url":"https://github.com/anandsuresh/smart-error/issues"},"license":"MIT","readme":"# smart-error\n\n[![node (scoped)](https://img.shields.io/node/v/@anandsuresh/smart-error.svg?style=plastic)](https://nodejs.org/en/download/)\n[![npm (scoped)](https://img.shields.io/npm/v/@anandsuresh/smart-error.svg?style=plastic)](https://www.npmjs.com/package/@anandsuresh/smart-error)\n[![npm](https://img.shields.io/npm/dt/@anandsuresh/smart-error.svg?style=plastic)](https://www.npmjs.com/package/@anandsuresh/smart-error)\n[![Travis](https://img.shields.io/travis/anandsuresh/smart-error.svg?style=plastic)](https://travis-ci.org/anandsuresh/smart-error)\n[![license](https://img.shields.io/github/license/anandsuresh/smart-error.svg?style=plastic)](LICENSE)\n[![GitHub followers](https://img.shields.io/github/followers/anandsuresh.svg?style=social&label=Follow)](https://github.com/anandsuresh)\n[![Twitter Follow](https://img.shields.io/twitter/follow/anandsuresh.svg?style=social&label=Follow)](https://twitter.com/intent/follow?screen_name=anandsuresh)\n\nA `SmartError` is a wrapper around the JavaScript `Error` class providing a few additional fields:\n\n- `code`: A unique code identifying the error\n- `metadata`: Optional metadata useful for debugging (e.g. the arguments passed to the function that threw the error)\n- `cause`: An optional error that is being wrapped by the `SmartError`\n\n## usage\n\n```\nconst {create} = require('@anandsuresh/smart-error')\n\nconst MyError = create('MyError', {\n  Unexpected: 'An unexpected error occurred.',\n  BadArguments: 'One or more arguments passed were invalid.',\n  TimedOut: 'A timeout occurred waiting for the operation to complete.'\n})\n\n\nfunction isOddNumber(number) {\n  if (typeof number !== 'number')\n    throw MyError.BadArguments(number)\n\n  return (number % 2 === 0)\n}\n\ntry {\n  console.log(`isOddNumber(3) = ${isOddNumber(3)}`)\n} catch (e) {\n  if (e.isBadArguments) {\n    console.error(`The argument $(e.metadata) is not a number!`)\n  } else {\n    console.error(e.toDetailedString())\n  }\n}\n```\n\n## api\n\n### `create(errorName, errors)`\n\n`create` takes 2 arguments:\n\n- `errorName`: the name of the error. E.g. `'MyError'`\n- `errors`: an object mapping error codes to error messages. E.g. `{Unexpected: 'An unexpected error occurred'}`\n\nThe `create()` function creates a static function for each error code and an getter function to check if that is the specific error wrapped by the `SmartError`. Consider the following example:\n\n```\nconst {create} = require('@anandsuresh/smart-error')\n\nconst MyError = create('MyError', {\n  Unexpected: 'An unexpected error occurred.',\n  BadArguments: 'One or more arguments passed were invalid.',\n  TimedOut: 'A timeout occurred waiting for the operation to complete.'\n})\n```\n\nThe newly created `MyError` class will inherit from the JavaScript `Error` class and have the following **instance** properties:\n\n- `name`: The name of the error, in this case `'MyError'`\n- `code`: The unique code of the error (`Unexpected`, `BadArguments`, `TimedOut`)\n- `message`: A human-readable error message\n- `metadata`: Optional metadata associated with the error (e.g. the arguments passed to the function that threw the error)\n- `cause`: An optional error that is being wrapped by the `SmartError`\n- `stack`: The stack-trace generated by the error\n- `isSmartError`: Helper property that wraps an `instanceof` check to return whether or not the instance is a `SmartError`\n- `isUnexpected`: Helper property that returns `true` if the `MyError` instance has the code `Unexpected`\n- `isBadArguments`: Helper property that returns `true` if the `MyError` instance has the code `BadArguments`\n- `isTimedOut`: Helper property that returns `true` if the `MyError` instance has the code `TimedOut`\n\n`MyError` will also receive the following set of **class** methods:\n\n- `Unexpected(metadata, cause)`: Creates an returns an instance of `MyError` with the `code` property set to `Unexpected`\n- `BadArguments(metadata, cause)`: Creates an returns an instance of `MyError` with the `code` property set to `BadArguments`\n- `TimedOut(metadata, cause)`: Creates an returns an instance of `MyError` with the `code` property set to `TimedOut`\n","readmeFilename":"README.md"}