- 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
// Throw an error with formatted message
throw ono("%s is invalid. Must be at least %d characters.", username, minLength);
// Wrap and re-throw an error without losing the original error's message and stack
throw ono(err, "An error occurred while saving your changes");
// Throw an error with custom properties (even a custom method!)
throw ono({code: 413, status: "Invalid data", retry: function() {...}});
// Add custom properties to an existing Error
throw ono(err, {code: 413, status: "Invalid data", retry: function() {...}})
// Any of the above can throw a specific Error subtype instead
throw ono.range(...); // RangeError
throw ono.syntax(...); // SyntaxError
throw ono.reference(...); // ReferenceError
Install using npm:
npm install ono
Then require it in your code:
var ono = require("ono");
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>
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. Seeono.formatter
for more info.
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.
Method | Error type |
---|---|
ono.error() |
Error (this is just an alternate syntax) |
ono.eval() |
EvalError |
ono.range() |
RangeError |
ono.reference() |
ReferenceError |
ono.syntax() |
SyntaxError |
ono.type() |
TypeError |
ono.uri() |
URIError |
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);
I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request.
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
Ono is 100% free and open-source, under the MIT license. Use it however you want.