@jsdevtools/ono
Advanced tools
Comparing version 7.1.1 to 7.1.2
{ | ||
"name": "@jsdevtools/ono", | ||
"version": "7.1.1", | ||
"version": "7.1.2", | ||
"description": "Throw better errors.", | ||
@@ -59,18 +59,18 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/polyfill": "^7.8.3", | ||
"@jsdevtools/eslint-config-modular": "^8.0.0", | ||
"@jsdevtools/host-environment": "^2.0.2", | ||
"@jsdevtools/karma-config": "^3.1.2", | ||
"@jsdevtools/tslint-modular": "^2.0.1", | ||
"@jsdevtools/version-bump-prompt": "^6.0.1", | ||
"@types/node": "^13.7.7", | ||
"@babel/polyfill": "^7.8.7", | ||
"@jsdevtools/eslint-config-modular": "^8.0.3", | ||
"@jsdevtools/host-environment": "^2.0.3", | ||
"@jsdevtools/karma-config": "^3.1.4", | ||
"@jsdevtools/tslint-modular": "^2.0.5", | ||
"@jsdevtools/version-bump-prompt": "^6.0.3", | ||
"@types/node": "^13.13.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^6.8.0", | ||
"karma": "^4.4.1", | ||
"karma": "^5.0.2", | ||
"karma-cli": "^2.0.0", | ||
"mocha": "^7.1.0", | ||
"mocha": "^7.1.1", | ||
"npm-check": "^5.9.2", | ||
"nyc": "^15.0.0", | ||
"nyc": "^15.0.1", | ||
"shx": "^0.3.2", | ||
"tslint": "^6.0.0", | ||
"tslint": "^6.1.1", | ||
"typescript": "^3.8.3", | ||
@@ -77,0 +77,0 @@ "typescript-tslint-plugin": "^0.5.5" |
@@ -27,3 +27,3 @@ ono (Oh No!) | ||
- Create Ono instances for your own [custom error classes](#custom-error-classes) | ||
- Supports and enhances your own [custom error classes](#custom-error-classes) | ||
@@ -283,4 +283,9 @@ - Tested on Node.js and all modern web browsers on Mac, Windows, and Linux. | ||
----------------------------- | ||
Ono has built-in support for all of [the built-in JavaScript Error types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types). For example, you can use `ono.reference()` to create a `ReferenceError`, or `ono.syntax()` to create a `SyntaxError`. In addition to the built-in types, you can also create Ono methods for your own custom error classes. | ||
There are two ways to use Ono with your own custom error classes. Which one you choose depends on what parameters your custom error class accepts, and whether you'd prefer to use `ono.myError()` syntax or `new MyError()` syntax. | ||
### Option 1: Standard Errors | ||
Ono has built-in support for all of [the built-in JavaScript Error types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types). For example, you can use `ono.reference()` to create a `ReferenceError`, or `ono.syntax()` to create a `SyntaxError`. | ||
All of these built-in JavaScript Error types accept a single parameter: the error message string. If your own error classes also work this way, then you can create Ono methods for your custom error classes. Here's an example: | ||
```javascript | ||
@@ -320,7 +325,39 @@ const { ono, Ono } = require("@jsdevtools/ono"); | ||
### Option 2: Enhanced Error Classes | ||
If your custom error classes require more than just an error message string parameter, then you'll need to use Ono differently. Rather than creating a [custom Ono method](#option-1-standard-errors) and using `ono.myError()` syntax, you'll use Ono _inside_ your error class's constructor. This has a few benefits: | ||
- Your error class can accept whatever parameters you want | ||
- Ono is encapsulated within your error class | ||
- You can use `new MyError()` syntax rather than `ono.myError()` syntax | ||
```javascript | ||
const { ono, Ono } = require("@jsdevtools/ono"); | ||
// A custom Error class for 404 Not Found | ||
class NotFoundError extends Error { | ||
constructor(method, url) { | ||
super(`404: ${method} ${url} was not found`); | ||
// Add custom properties, enhance JSON.stringify() support, etc. | ||
Ono.extend(this, { statusCode: 404, method, url }); | ||
} | ||
} | ||
// A custom Error class for 500 Server Error | ||
class ServerError extends Error { | ||
constructor(originalError, method, url) { | ||
super(`500: A server error occurred while responding to ${method} ${url}`); | ||
// Append the stack trace and custom properties of the original error, | ||
// and add new custom properties, enhance JSON.stringify() support, etc. | ||
Ono.extend(this, originalError, { statusCode: 500, method, url }); | ||
} | ||
} | ||
``` | ||
Contributing | ||
-------------------------- | ||
Contributions, enhancements, and bug-fixes are welcome! [File an issue](https://github.com/JS-DevTools/ono/issues) on GitHub and [submit a pull request](https://github.com/JS-DevTools/ono/pulls). | ||
Contributions, enhancements, and bug-fixes are welcome! [Open an issue](https://github.com/JS-DevTools/ono/issues) on GitHub and [submit a pull request](https://github.com/JS-DevTools/ono/pulls). | ||
@@ -327,0 +364,0 @@ #### Building/Testing |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104823
393