What is ono?
The 'ono' npm package is a utility for creating and managing errors in JavaScript. It allows you to create custom error messages, wrap existing errors, and add additional context to errors. This can be particularly useful for debugging and logging purposes.
What are ono's main functionalities?
Creating Custom Errors
This feature allows you to create custom error messages. The 'ono' function can be used to generate a new error with a specific message.
const ono = require('ono');
const error = ono('Something went wrong!');
console.error(error);
Wrapping Existing Errors
This feature allows you to wrap existing errors with additional context. This can be useful for adding more information to errors that are caught during execution.
const ono = require('ono');
try {
JSON.parse('invalid JSON');
} catch (err) {
const wrappedError = ono(err, 'Failed to parse JSON');
console.error(wrappedError);
}
Adding Context to Errors
This feature allows you to add additional context to errors, such as custom properties. In this example, a status code is added to the error object.
const ono = require('ono');
const error = ono({ status: 404 }, 'Resource not found');
console.error(error);
console.error(error.status);
Other packages similar to ono
verror
The 'verror' package provides similar functionality for creating and managing errors. It allows you to create rich error objects with additional context and stack traces. Compared to 'ono', 'verror' offers more advanced features for error propagation and chaining.
create-error
The 'create-error' package is another alternative for creating custom error types. It allows you to define new error classes with custom properties and methods. While 'ono' focuses on wrapping and adding context to errors, 'create-error' is more about defining new error types.
custom-error-generator
The 'custom-error-generator' package allows you to generate custom error classes with specific properties and methods. It is similar to 'create-error' but offers more flexibility in defining the error structure. Compared to 'ono', it is more focused on error class generation rather than error wrapping and context.
ono (Oh No!)
Throw better errors.
Features
- Formatted error messages, using Node's
util.format()
or your own custom formatter - Wrap and re-throw an error without losing the original error's message, stack trace, and properties
- Add custom properties to your errors — great for error codes, support numbers, help URLs, etc.
- Errors can be serialized as JSON, including all native and custom properties
- Tested on Node.js and all modern web browsers on Mac, Windows, Linux, iOS, and Android
Example
throw ono("%s is invalid. Must be at least %d characters.", username, minLength);
throw ono(err, "An error occurred while saving your changes");
throw ono({code: 413, status: "Invalid data", retry: function() {...}});
throw ono(err, {code: 413, status: "Invalid data", retry: function() {...}})
throw ono.range(...);
throw ono.syntax(...);
throw ono.reference(...);
Installation
Node
Install using npm:
npm install ono
Then require it in your code:
var ono = require("ono");
Web Browsers
Reference ono.js
or ono.min.js
in your HTML:
<script src="https://cdn.rawgit.com/JS-DevTools/ono/master/dist/ono.js"></script>
<script>
var timestamp = new Date().toISOString();
throw ono('This error was thrown at %s', timestamp);
</script>
API
ono([err], [props], [message, ...])
Creates an Error
object with the given properties.
-
err
- (optional) An existing error object. This error's message, stack trace, and properties will be appended to the new error.
-
props
- (optional) An object whose properties will be added to the new error. Properties can be anything, including objects and functions.
-
message
- (optional) The error message string. If it contains placeholders, then pass each placeholder's value as an additional parameter. See ono.formatter
for more info.
Specific error types
The default ono()
function always creates Error
objects, but you can use any of the following methods to explicitly create the corresponding Error subclass. The method signatures are exactly the same as above.
ono.formatter
By default, Node's util.format()
function is used (even in browsers) to format error messages and substitute placeholders with their corresponding values. You can set ono.formatter
to a third-party formatter or even your own custom implementation, like this:
ono.formatter = function(message) {
var params = Array.prototype.slice.call(arguments, 1);
return params.reduce(function(message, param, index) {
return message.replace("$" + index, param);
}, message);
}
throw ono("$0 must be greater than $1", 4, 10);
Contributing
I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request.
Building/Testing
To build/test the project locally on your computer:
-
Clone this repo
git clone https://github.com/JS-DevTools/ono.git
-
Install dependencies
npm install
-
Run the build script
npm run build
-
Run the tests
npm test
License
Ono is 100% free and open-source, under the MIT license. Use it however you want.