You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-3Lines changed: 40 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Features
24
24
25
25
- Enhanced support for [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) and [`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)— great for logging
26
26
27
-
-Create Ono instances for your own [custom error classes](#custom-error-classes)
27
+
-Supports and enhances your own [custom error classes](#custom-error-classes)
28
28
29
29
- Tested on Node.js and all modern web browsers on Mac, Windows, and Linux.
30
30
@@ -279,7 +279,12 @@ throw ono("$0 is invalid. Must be at least $1 characters.", username, minLength)
279
279
280
280
Custom Error Classes
281
281
-----------------------------
282
-
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.
282
+
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.
283
+
284
+
### Option 1: Standard Errors
285
+
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`.
286
+
287
+
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:
283
288
284
289
```javascript
285
290
const { ono, Ono } =require("@jsdevtools/ono");
@@ -315,11 +320,43 @@ The code above throws an instance of `MyErrorClass` that looks like this:
315
320
}
316
321
```
317
322
323
+
### Option 2: Enhanced Error Classes
324
+
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:
325
+
326
+
- Your error class can accept whatever parameters you want
327
+
- Ono is encapsulated within your error class
328
+
- You can use `new MyError()` syntax rather than `ono.myError()` syntax
329
+
330
+
```javascript
331
+
const { ono, Ono } =require("@jsdevtools/ono");
332
+
333
+
// A custom Error class for 404 Not Found
334
+
classNotFoundErrorextendsError {
335
+
constructor(method, url) {
336
+
super(`404: ${method}${url} was not found`);
337
+
338
+
// Add custom properties, enhance JSON.stringify() support, etc.
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).
359
+
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).
323
360
324
361
#### Building/Testing
325
362
To build/test the project locally on your computer:
0 commit comments