https://project.mdnd-it.cc/work_packages/94
This commit is contained in:
2025-08-23 04:25:28 +02:00
parent 725516ad6c
commit 19cfa031d0
25823 changed files with 1095587 additions and 2801760 deletions
+122
View File
@@ -0,0 +1,122 @@
Change Log
=======================================
All notable changes will be documented in this file.
`ono` adheres to [Semantic Versioning](http://semver.org/).
[v7.1.0](https://github.com/JS-DevTools/ono/tree/v7.1.0) (2020-03-03)
----------------------------------------------------------------------------------------------------
- Added a static `Ono.extend()` method that allows Ono to extend errors that were created outside of Ono. The extended error gets all the Ono functionality, including nested stack traces, custom properties, improved support for `JSON.stringify()`, etc.
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v7.0.1...v7.1.0)
[v7.0.0](https://github.com/JS-DevTools/ono/tree/v7.0.0) (2020-02-16)
----------------------------------------------------------------------------------------------------
- Moved Ono to the [@JSDevTools scope](https://www.npmjs.com/org/jsdevtools) on NPM
- The "ono" NPM package is now just a wrapper around the scoped "@jsdevtools/ono" package
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v6.0.1...v7.0.0)
[v6.0.0](https://github.com/JS-DevTools/ono/tree/v6.0.0) (2019-12-28)
----------------------------------------------------------------------------------------------------
### Breaking Changes
- Dropped support for IE8 and other JavaScript engines that don't support [`Object.getOwnPropertyDescriptor()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor)
- Removed `ono.formatter`. It has been replaced with [the `format` option](https://github.com/JS-DevTools/ono#format-option)
- When using the default `ono()` function to wrap an error, it will now try to match the error's type, rather than simply using the base `Error` class.
### New Features
- The [`Ono` constructor](https://github.com/JS-DevTools/ono#onoerror-options) now accepts an optional [options parameter](https://github.com/JS-DevTools/ono#options), which lets you customize the behavior of Ono
- The [`concatMessages` option](https://github.com/JS-DevTools/ono#concatmessages-option) lets you control whether the original error's message is appended to your error message
- The [`format` option](https://github.com/JS-DevTools/ono#format-option) lets you provide a custom function for replacing placeholders in error messages
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v5.1.0...v6.0.0)
[v5.1.0](https://github.com/JS-DevTools/ono/tree/v5.1.0) (2019-09-10)
----------------------------------------------------------------------------------------------------
- Added a static `Ono.toJSON()` method that accepts any `Error` (even a non-Ono error) and returns a POJO that can be used with `JSON.stringify()`. Ono errors already have a built-in `toJSON()` method, but this exposes that enhanced functionality in a way that can be used with _any_ error.
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v5.0.2...v5.1.0)
[v5.0.0](https://github.com/JS-DevTools/ono/tree/v5.0.0) (2019-02-18)
----------------------------------------------------------------------------------------------------
### Breaking Changes
#### in Node.js
- Ono errors previously included an `inspect()` method to support Node's [`util.inspect()` function](https://nodejs.org/api/util.html#util_util_inspect_object_options). As of Node v6.6.0, the `inspect()` method is deprecated in favor of a [`util.inspect.custom`](https://nodejs.org/api/util.html#util_util_inspect_custom). Ono has updated accordingly, so errors no longer have an `inspect()` method.
#### in Web Browsers
- We no longer automatically include a polyfill for [Node's `util.format()` function](https://nodejs.org/api/util.html#util_util_format_format_args). We recommend using [ES6 template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) instead. Or you can import [a polyfill](https://github.com/tmpfs/format-util) yourself and assign it to [the `ono.formatter` property](https://jstools.dev/ono/#onoformatter).
### New Features
- Completely rewritten in TypeScript.
- Ono is now completely dependency free.
- You can now create your own Ono functions for custom error classes. See [the docs](https://jstools.dev/ono/#custom-error-classes) for details.
- Symbol-keyed properties are now supported. If the `originalError` and/or `props` objects has Symbol-keyed properties, they will be copied to the Ono error.
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v4.0.11...v5.0.0)
[v4.0.0](https://github.com/JS-DevTools/ono/tree/v4.0.0) (2017-07-07)
----------------------------------------------------------------------------------------------------
The `err` parameter (see [the API docs](https://github.com/JS-DevTools/ono#api)) can now be any type of object, not just an `instanceof Error`. This allows for errors that don't extend from the `Error` class, such as [`DOMError`](https://developer.mozilla.org/en-US/docs/Web/API/DOMError), [`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException), and custom error types.
> **NOTE:** This should **not** be a breaking change, but I'm bumping the major version number out of an abundance of caution.
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v3.1.0...v4.0.0)
[v3.1.0](https://github.com/JS-DevTools/ono/tree/v3.1.0) (2017-06-01)
----------------------------------------------------------------------------------------------------
We removed the direct dependency on [Node's `util.format()`](https://nodejs.org/api/util.html#util_util_format_format_args), which was needlessly bloating the browser bundle. Instead, I now import [`format-util`](https://www.npmjs.com/package/format-util), which a much more [lightweight browser implementation](https://github.com/tmpfs/format-util/blob/f88c550ef10c5aaadc15a7ebab595f891bb385e1/format.js). There's no change when running in Node.js, because `format-util` simply [exports `util.format()`](https://github.com/tmpfs/format-util/blob/392628c5d45e558589f2f19ffb9d79d4b5540010/index.js#L1).
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v3.0.0...v3.1.0)
[v3.0.0](https://github.com/JS-DevTools/ono/tree/v3.0.0) (2017-06-01)
----------------------------------------------------------------------------------------------------
- Updated all dependencies and verified support for Node 8.0
- Ono no longer appears in error stack traces, so errors look like they came directly from your code
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v2.0.0...v3.0.0)
[v2.0.0](https://github.com/JS-DevTools/ono/tree/v2.0.0) (2015-12-14)
----------------------------------------------------------------------------------------------------
- Did a major refactoring and code cleanup
- Support for various browser-specific `Error.prototype` properties (`fileName`, `lineNumber`, `sourceURL`, etc.)
- If you define a custom `toJSON()` method on an error object, Ono will no longer overwrite it
- Added support for Node's `util.inspect()`
[Full Changelog](https://github.com/JS-DevTools/ono/compare/v1.0.22...v2.0.0)
+23
View File
@@ -0,0 +1,23 @@
The MIT License (MIT)
Copyright (c) 2015 James Messinger
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
.
+392
View File
@@ -0,0 +1,392 @@
ono (Oh No!)
============================
### Throw better errors.
[![npm](https://img.shields.io/npm/v/@jsdevtools/ono.svg)](https://www.npmjs.com/package/@jsdevtools/ono)
[![License](https://img.shields.io/npm/l/@jsdevtools/ono.svg)](LICENSE)
[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/JS-DevTools/ono)
[![Build Status](https://github.com/JS-DevTools/ono/workflows/CI-CD/badge.svg)](https://github.com/JS-DevTools/ono/actions)
[![Coverage Status](https://coveralls.io/repos/github/JS-DevTools/ono/badge.svg?branch=master)](https://coveralls.io/github/JS-DevTools/ono)
[![Dependencies](https://david-dm.org/JS-DevTools/ono.svg)](https://david-dm.org/JS-DevTools/ono)
[![OS and Browser Compatibility](https://jstools.dev/img/badges/ci-badges-with-ie.svg)](https://github.com/JS-DevTools/ono/actions)
Features
--------------------------
- Wrap and re-throw an error _without_ losing the original error's type, message, stack trace, and properties
- Add custom properties to errors — great for error numbers, status codes, etc.
- Use [format strings](#format-option) for error messages — great for localization
- 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
- Supports and enhances your own [custom error classes](#custom-error-classes)
- Tested on Node.js and all modern web browsers on Mac, Windows, and Linux.
Example
--------------------------
```javascript
const ono = require("@jsdevtools/ono");
// Throw an error with custom properties
throw ono({ code: "NOT_FOUND", status: 404 }, `Resource not found: ${url}`);
// Wrap an error without losing the original error's stack and props
throw ono(originalError, "An error occurred while saving your changes");
// Wrap an error and add custom properties
throw ono(originalError, { code: 404, status: "NOT_FOUND" });
// Wrap an error, add custom properties, and change the error message
throw ono(originalError, { code: 404, status: "NOT_FOUND" }, `Resource not found: ${url}`);
// Throw a specific Error subtype instead
// (works with any of the above signatures)
throw ono.range(...); // RangeError
throw ono.syntax(...); // SyntaxError
throw ono.reference(...); // ReferenceError
// Create an Ono method for your own custom error class
const { Ono } = require("@jsdevtools/ono");
class MyErrorClass extends Error {}
ono.myError = new Ono(MyErrorClass);
// And use it just like any other Ono method
throw ono.myError(...); // MyErrorClass
```
Installation
--------------------------
Install using [npm](https://docs.npmjs.com/about-npm/):
```bash
npm install @jsdevtools/ono
```
Usage
--------------------------
When using Ono in Node.js apps, you'll probably want to use **CommonJS** syntax:
```javascript
const ono = require("@jsdevtools/ono");
```
When using a transpiler such as [Babel](https://babeljs.io/) or [TypeScript](https://www.typescriptlang.org/), or a bundler such as [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/), you can use **ECMAScript modules** syntax instead:
```javascript
import ono from "@jsdevtools/ono";
```
Browser support
--------------------------
Ono supports recent versions of every major web browser. Older browsers may require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill).
To use Ono in a browser, you'll need to use a bundling tool such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve).
API
--------------------------
### `ono([originalError], [props], [message, ...])`
Creates an [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object with the given properties.
* `originalError` - _(optional)_ The original error that occurred, if any. This error's message, stack trace, and properties will be copied to the new error. If this error's type is one of the [known error types](#specific-error-types), then the new error will be of the same type.
* `props` - _(optional)_ An object whose properties will be copied 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 the [`format` option](#format-option) for more info.
### Specific error types
The default `ono()` function may return an instance of the base [`Error` class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error), or it may return a more specific sub-class, based on the type of the `originalError` argument. If you want to _explicitly_ create a specific type of error, then you can use any of the following methods:
The method signatures and arguments are exactly the same as [the default `ono()` function](#onooriginalerror-props-message-).
|Method | Return Type
|:---------------------------|:-------------------
|`ono.error()` |[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
|`ono.eval()` |[`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError)
|`ono.range()` |[`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError)
|`ono.reference()` |[`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError)
|`ono.syntax()` |[`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)
|`ono.type()` |[`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError)
|`ono.uri()` |[`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError)
|`ono.yourCustomErrorHere()` |Add your own [custom error classes](#custom-error-classes) to ono
### `Ono(Error, [options])`
The `Ono` constructor is used to create your own [custom `ono` methods](#custom-error-classes) for custom error types, or to change the default behavior of the built-in methods.
> **Warning:** Be sure not to confuse `ono` (lowercase) and `Ono` (capitalized). The latter one is a class.
* `Error` - The Error sub-class that this Ono method will create instances of
* `options` - _(optional)_ An [options object](#options), which customizes the behavior of the Ono method
Options
---------------------------------
The `Ono` constructor takes an optional options object as a second parameter. The object can have the following properties, all of which are optional:
|Option |Type |Default |Description
|-----------------|------------|-------------|---------------------------------------------------------------
|`concatMessages` |boolean |`true` |When Ono is used to wrap an error, this setting determines whether the inner error's message is appended to the new error message.
|`format` |function or boolean |[`util.format()`](https://nodejs.org/api/util.html#util_util_format_format_args) in Node.js<br><br>`false` in web browsers|A function that replaces placeholders like in error messages with values.<br><br>If set to `false`, then error messages will be treated as literals and no placeholder replacement will occur.
### `concatMessages` Option
When wrapping an error, Ono's default behavior is to append the error's message to your message, with a newline between them. For example:
```javascript
const ono = require("@jsdevtools/ono");
function createArray(length) {
try {
return new Array(length);
}
catch (error) {
// Wrap and re-throw the error
throw ono(error, "Sorry, I was unable to create the array.");
}
}
// Try to create an array with a negative length
createArray(-5);
```
The above code produces the following error message:
```
Sorry, I was unable to create the array.
Invalid array length;
```
If you'd rather not include the original message, then you can set the `concatMessages` option to `false`. For example:
```javascript
const { ono, Ono } = require("@jsdevtools/ono");
// Override the default behavior for the RangeError
ono.range = new Ono(RangeError, { concatMessages: false });
function createArray(length) {
try {
return new Array(length);
}
catch (error) {
// Wrap and re-throw the error
throw ono(error, "Sorry, I was unable to create the array.");
}
}
// Try to create an array with a negative length
createArray(-5);
```
Now the error only includes your message, not the original error message.
```
Sorry, I was unable to create the array.
```
### `format` option
The `format` option let you set a format function, which replaces placeholders in error messages with values.
When running in Node.js, Ono uses [the `util.format()` function](https://nodejs.org/api/util.html#util_util_format_format_args) by default, which lets you use placeholders such as %s, %d, and %j. You can provide the values for these placeholders when calling any Ono method:
```javascript
throw ono("%s is invalid. Must be at least %d characters.", username, minLength);
```
Of course, the above example could be accomplished using [ES6 template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) instead of format strings:
```javascript
throw ono(`${username} is invalid. Must be at least ${minLength} characters.`);
```
Format strings are most useful when you don't alrady know the values at the time that you're writing the string. A common scenario is localization. Here's a simplistic example:
```javascript
const errorMessages {
invalidLength: {
en: "%s is invalid. Must be at least %d characters.",
es: "%s no es válido. Debe tener al menos %d caracteres.",
zh: "%s 无效。 必须至少%d个字符。",
}
}
let lang = getCurrentUsersLanguage();
throw ono(errorMessages.invalidLength[lang], username, minLength);
```
#### The `format` option in web browsers
Web browsers don't have a built-in equivalent of Node's [`util.format()` function](https://nodejs.org/api/util.html#util_util_format_format_args), so format strings are only supported in Node.js by default. However, you can set the `format` option to any compatible polyfill library to enable this functionality in web browsers too.
Here are some compatible polyfill libraries:
- [format](https://www.npmjs.com/package/format)
- [format-util](https://github.com/tmpfs/format-util)
#### Custom `format` implementation
If the standard [`util.format()`](https://nodejs.org/api/util.html#util_util_format_format_args) functionality isn't sufficient for your needs, then you can set the `format` option to your own custom implementation. Here's a simplistic example:
```javascript
const { ono, Ono } = require("@jsdevtools/ono");
// This is a simple formatter that replaces $0, $1, $2, ... with the corresponding argument
let options = {
format(message, ...args) {
for (let [index, arg] of args.entries()) {
message = message.replace("$" + index, arg);
}
return message;
}
};
// Use your custom formatter for all of the built-in error types
ono.error = new Ono(Error, options);
ono.eval = new Ono(EvalError, options);
ono.range = new Ono(RangeError, options);
ono.reference = new Ono(ReferenceError, options);
ono.syntax = new Ono(SyntaxError, options);
ono.type = new Ono(TypeError, options);
ono.uri = new Ono(URIError, options);
// Now all Ono functions support your custom formatter
throw ono("$0 is invalid. Must be at least $1 characters.", username, minLength);
```
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
const { ono, Ono } = require("@jsdevtools/ono");
let counter = 0;
// A custom Error class that assigns a unique ID and timestamp to each error
class MyErrorClass extends Error {
constructor(message) {
super(message);
this.id = ++counter;
this.timestamp = new Date();
}
}
// Create a new Ono method for your custom Error class
ono.myError = new Ono(MyErrorClass);
// You can use this method just like any other Ono method
throw ono.myError({ code: 404, status: "NOT_FOUND" }, `Resource not found: ${url}`);
```
The code above throws an instance of `MyErrorClass` that looks like this:
```javascript
{
"name": "MyErrorClass",
"message": "Resource not found: xyz.html",
"id": 1,
"timestamp": "2019-01-01T12:30:00.456Z",
"code": 404,
"status": "NOT_FOUND",
"stack": "MyErrorClass: Resource not found: xyz.html\n at someFunction (index.js:24:5)",
}
```
### 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! [Open an issue](https://github.com/JS-DevTools/ono/issues) on GitHub and [submit a pull request](https://github.com/JS-DevTools/ono/pulls).
#### Building/Testing
To build/test the project locally on your computer:
1. __Clone this repo__<br>
`git clone https://github.com/JS-DevTools/ono.git`
2. __Install dependencies__<br>
`npm install`
3. __Run the build script__<br>
`npm run build`
4. __Run the tests__<br>
`npm test`
License
--------------------------
Ono is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/JS-DevTools/ono) to thank us for our work. By contributing to the Treeware forest youll be creating employment for local families and restoring wildlife habitats.
Big Thanks To
--------------------------
Thanks to these awesome companies for their support of Open Source developers ❤
[![Travis CI](https://jstools.dev/img/badges/travis-ci.svg)](https://travis-ci.com)
[![SauceLabs](https://jstools.dev/img/badges/sauce-labs.svg)](https://saucelabs.com)
[![Coveralls](https://jstools.dev/img/badges/coveralls.svg)](https://coveralls.io)
@@ -0,0 +1,3 @@
import { OnoConstructor } from "./types";
declare const constructor: OnoConstructor;
export { constructor as Ono };
+47
View File
@@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ono = void 0;
const extend_error_1 = require("./extend-error");
const normalize_1 = require("./normalize");
const to_json_1 = require("./to-json");
const constructor = Ono;
exports.Ono = constructor;
/**
* Creates an `Ono` instance for a specifc error type.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
function Ono(ErrorConstructor, options) {
options = normalize_1.normalizeOptions(options);
function ono(...args) {
let { originalError, props, message } = normalize_1.normalizeArgs(args, options);
// Create a new error of the specified type
let newError = new ErrorConstructor(message);
// Extend the error with the properties of the original error and the `props` object
return extend_error_1.extendError(newError, originalError, props);
}
ono[Symbol.species] = ErrorConstructor;
return ono;
}
/**
* Returns an object containing all properties of the given Error object,
* which can be used with `JSON.stringify()`.
*/
Ono.toJSON = function toJSON(error) {
return to_json_1.toJSON.call(error);
};
/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
* additional properties, and improved support for `JSON.stringify()`.
*/
Ono.extend = function extend(error, originalError, props) {
if (props || originalError instanceof Error) {
return extend_error_1.extendError(error, originalError, props);
}
else if (originalError) {
return extend_error_1.extendError(error, undefined, originalError);
}
else {
return extend_error_1.extendError(error);
}
};
//# sourceMappingURL=constructor.js.map
@@ -0,0 +1 @@
{"version":3,"file":"constructor.js","sourceRoot":"","sources":["../src/constructor.ts"],"names":[],"mappings":";;;AAAA,iDAA6C;AAC7C,2CAA8D;AAC9D,uCAAkD;AAGlD,MAAM,WAAW,GAAG,GAAqB,CAAC;AAClB,0BAAG;AAE3B;;GAEG;AACH,gEAAgE;AAChE,SAAS,GAAG,CAAsB,gBAAyC,EAAE,OAAoB;IAC/F,OAAO,GAAG,4BAAgB,CAAC,OAAO,CAAC,CAAC;IAEpC,SAAS,GAAG,CAAwC,GAAG,IAAe;QACpE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,yBAAa,CAAO,IAAI,EAAE,OAAQ,CAAC,CAAC;QAE5E,2CAA2C;QAC3C,IAAI,QAAQ,GAAG,IAAK,gBAAiD,CAAC,OAAO,CAAC,CAAC;QAE/E,oFAAoF;QACpF,OAAO,0BAAW,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;IACrD,CAAC;IAED,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,gBAAgB,CAAC;IACvC,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,GAAG,CAAC,MAAM,GAAG,SAAS,MAAM,CAAC,KAAgB;IAC3C,OAAO,gBAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF;;;GAGG;AACH,GAAG,CAAC,MAAM,GAAG,SAAS,MAAM,CAAC,KAAgB,EAAE,aAAyB,EAAE,KAAc;IACtF,IAAI,KAAK,IAAI,aAAa,YAAY,KAAK,EAAE;QAC3C,OAAO,0BAAW,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;KACjD;SACI,IAAI,aAAa,EAAE;QACtB,OAAO,0BAAW,CAAC,KAAK,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;KACrD;SACI;QACH,OAAO,0BAAW,CAAC,KAAK,CAAC,CAAC;KAC3B;AACH,CAAC,CAAC"}
@@ -0,0 +1,9 @@
import { ErrorLike, OnoError } from "./types";
/**
* Extends the new error with the properties of the original error and the `props` object.
*
* @param newError - The error object to extend
* @param originalError - The original error object, if any
* @param props - Additional properties to add, if any
*/
export declare function extendError<T extends ErrorLike, E extends ErrorLike, P extends object>(error: T, originalError?: E, props?: P): T & E & P & OnoError<T & E & P>;
+77
View File
@@ -0,0 +1,77 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendError = void 0;
const isomorphic_node_1 = require("./isomorphic.node");
const stack_1 = require("./stack");
const to_json_1 = require("./to-json");
const protectedProps = ["name", "message", "stack"];
/**
* Extends the new error with the properties of the original error and the `props` object.
*
* @param newError - The error object to extend
* @param originalError - The original error object, if any
* @param props - Additional properties to add, if any
*/
function extendError(error, originalError, props) {
let onoError = error;
extendStack(onoError, originalError);
// Copy properties from the original error
if (originalError && typeof originalError === "object") {
mergeErrors(onoError, originalError);
}
// The default `toJSON` method doesn't output props like `name`, `message`, `stack`, etc.
// So replace it with one that outputs every property of the error.
onoError.toJSON = to_json_1.toJSON;
// On Node.js, add support for the `util.inspect()` method
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (isomorphic_node_1.addInspectMethod) {
isomorphic_node_1.addInspectMethod(onoError);
}
// Finally, copy custom properties that were specified by the user.
// These props OVERWRITE any previous props
if (props && typeof props === "object") {
Object.assign(onoError, props);
}
return onoError;
}
exports.extendError = extendError;
/**
* Extend the error stack to include its cause
*/
function extendStack(newError, originalError) {
let stackProp = Object.getOwnPropertyDescriptor(newError, "stack");
if (stack_1.isLazyStack(stackProp)) {
stack_1.lazyJoinStacks(stackProp, newError, originalError);
}
else if (stack_1.isWritableStack(stackProp)) {
newError.stack = stack_1.joinStacks(newError, originalError);
}
}
/**
* Merges properties of the original error with the new error.
*
* @param newError - The error object to extend
* @param originalError - The original error object, if any
*/
function mergeErrors(newError, originalError) {
// Get the original error's keys
// NOTE: We specifically exclude properties that we have already set on the new error.
// This is _especially_ important for the `stack` property, because this property has
// a lazy getter in some environments
let keys = to_json_1.getDeepKeys(originalError, protectedProps);
// HACK: We have to cast the errors to `any` so we can use symbol indexers.
// see https://github.com/Microsoft/TypeScript/issues/1863
let _newError = newError;
let _originalError = originalError;
for (let key of keys) {
if (_newError[key] === undefined) {
try {
_newError[key] = _originalError[key];
}
catch (e) {
// This property is read-only, so it can't be copied
}
}
}
}
//# sourceMappingURL=extend-error.js.map
@@ -0,0 +1 @@
{"version":3,"file":"extend-error.js","sourceRoot":"","sources":["../src/extend-error.ts"],"names":[],"mappings":";;;AAAA,uDAAqD;AACrD,mCAAmF;AACnF,uCAAgD;AAGhD,MAAM,cAAc,GAA2B,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAE5E;;;;;;GAMG;AACH,SAAgB,WAAW,CAA6D,KAAQ,EAAE,aAAiB,EAAE,KAAS;IAC5H,IAAI,QAAQ,GAAG,KAAmD,CAAC;IAEnE,WAAW,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;IAErC,0CAA0C;IAC1C,IAAI,aAAa,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QACtD,WAAW,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;KACtC;IAED,yFAAyF;IACzF,mEAAmE;IACnE,QAAQ,CAAC,MAAM,GAAG,gBAAM,CAAC;IAEzB,0DAA0D;IAC1D,uEAAuE;IACvE,IAAI,kCAAgB,EAAE;QACpB,kCAAgB,CAAC,QAAQ,CAAC,CAAC;KAC5B;IAED,mEAAmE;IACnE,2CAA2C;IAC3C,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACtC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;KAChC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AA3BD,kCA2BC;AAED;;GAEG;AACH,SAAS,WAAW,CAAC,QAAmB,EAAE,aAAyB;IACjE,IAAI,SAAS,GAAG,MAAM,CAAC,wBAAwB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAEnE,IAAI,mBAAW,CAAC,SAAS,CAAC,EAAE;QAC1B,sBAAc,CAAC,SAAS,EAAE,QAAQ,EAAE,aAAa,CAAC,CAAC;KACpD;SACI,IAAI,uBAAe,CAAC,SAAS,CAAC,EAAE;QACnC,QAAQ,CAAC,KAAK,GAAG,kBAAU,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;KACtD;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,QAAmB,EAAE,aAAwB;IAChE,gCAAgC;IAChC,sFAAsF;IACtF,qFAAqF;IACrF,qCAAqC;IACrC,IAAI,IAAI,GAAG,qBAAW,CAAC,aAAa,EAAE,cAAc,CAAC,CAAC;IAEtD,2EAA2E;IAC3E,0DAA0D;IAC1D,IAAI,SAAS,GAAG,QAAe,CAAC;IAChC,IAAI,cAAc,GAAG,aAAoB,CAAC;IAE1C,KAAK,IAAI,GAAG,IAAI,IAAI,EAAE;QACpB,IAAI,SAAS,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;YAChC,IAAI;gBACF,SAAS,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;aACtC;YACD,OAAO,CAAC,EAAE;gBACR,oDAAoD;aACrD;SACF;KACF;AACH,CAAC"}
+5
View File
@@ -0,0 +1,5 @@
import { ono } from "./singleton";
export { Ono } from "./constructor";
export * from "./types";
export { ono };
export default ono;
+25
View File
@@ -0,0 +1,25 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ono = void 0;
/* eslint-env commonjs */
const singleton_1 = require("./singleton");
Object.defineProperty(exports, "ono", { enumerable: true, get: function () { return singleton_1.ono; } });
var constructor_1 = require("./constructor");
Object.defineProperty(exports, "Ono", { enumerable: true, get: function () { return constructor_1.Ono; } });
__exportStar(require("./types"), exports);
exports.default = singleton_1.ono;
// CommonJS default export hack
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = Object.assign(module.exports.default, module.exports);
}
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,yBAAyB;AACzB,2CAAkC;AAIzB,oFAJA,eAAG,OAIA;AAFZ,6CAAoC;AAA3B,kGAAA,GAAG,OAAA;AACZ,0CAAwB;AAGxB,kBAAe,eAAG,CAAC;AAEnB,+BAA+B;AAC/B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,EAAE;IACpE,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;CACxE"}
@@ -0,0 +1,15 @@
/**
* Ono supports custom formatters for error messages. In Node.js, it defaults
* to the `util.format()` function. In browsers, it defaults to `Array.join()`.
*
* The Node.js functionality can be used in a web browser via a polyfill,
* such as "format-util".
*
* @see https://github.com/tmpfs/format-util
*/
export declare const format = false;
/**
* The `util.inspect()` functionality only applies to Node.js.
* We return the constant `false` here so that the Node-specific code gets removed by tree-shaking.
*/
export declare const addInspectMethod = false;
@@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addInspectMethod = exports.format = void 0;
/**
* Ono supports custom formatters for error messages. In Node.js, it defaults
* to the `util.format()` function. In browsers, it defaults to `Array.join()`.
*
* The Node.js functionality can be used in a web browser via a polyfill,
* such as "format-util".
*
* @see https://github.com/tmpfs/format-util
*/
exports.format = false;
/**
* The `util.inspect()` functionality only applies to Node.js.
* We return the constant `false` here so that the Node-specific code gets removed by tree-shaking.
*/
exports.addInspectMethod = false;
//# sourceMappingURL=isomorphic.browser.js.map
@@ -0,0 +1 @@
{"version":3,"file":"isomorphic.browser.js","sourceRoot":"","sources":["../src/isomorphic.browser.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;GAQG;AACU,QAAA,MAAM,GAAG,KAAK,CAAC;AAE5B;;;GAGG;AACU,QAAA,gBAAgB,GAAG,KAAK,CAAC"}
@@ -0,0 +1,15 @@
/// <reference types="node" />
import * as util from "util";
import { OnoError } from "./types";
/**
* Ono supports Node's `util.format()` formatting for error messages.
*
* @see https://nodejs.org/api/util.html#util_util_format_format_args
*/
export declare const format: typeof util.format;
/**
* Adds an `inspect()` method to support Node's `util.inspect()` function.
*
* @see https://nodejs.org/api/util.html#util_util_inspect_custom
*/
export declare function addInspectMethod<T>(newError: OnoError<T>): void;
@@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addInspectMethod = exports.format = void 0;
const util = require("util");
const to_json_1 = require("./to-json");
// The `inspect()` method is actually a Symbol, not a string key.
// https://nodejs.org/api/util.html#util_util_inspect_custom
const inspectMethod = util.inspect.custom || Symbol.for("nodejs.util.inspect.custom");
/**
* Ono supports Node's `util.format()` formatting for error messages.
*
* @see https://nodejs.org/api/util.html#util_util_format_format_args
*/
exports.format = util.format;
/**
* Adds an `inspect()` method to support Node's `util.inspect()` function.
*
* @see https://nodejs.org/api/util.html#util_util_inspect_custom
*/
function addInspectMethod(newError) {
// @ts-expect-error - TypeScript doesn't support symbol indexers
newError[inspectMethod] = inspect;
}
exports.addInspectMethod = addInspectMethod;
/**
* Returns a representation of the error for Node's `util.inspect()` method.
*
* @see https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
*/
function inspect() {
// HACK: We have to cast the objects to `any` so we can use symbol indexers.
// see https://github.com/Microsoft/TypeScript/issues/1863
let pojo = {};
let error = this;
for (let key of to_json_1.getDeepKeys(error)) {
let value = error[key];
pojo[key] = value;
}
// Don't include the `inspect()` method on the output object,
// otherwise it will cause `util.inspect()` to go into an infinite loop
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete pojo[inspectMethod];
return pojo;
}
//# sourceMappingURL=isomorphic.node.js.map
@@ -0,0 +1 @@
{"version":3,"file":"isomorphic.node.js","sourceRoot":"","sources":["../src/isomorphic.node.ts"],"names":[],"mappings":";;;AAAA,6BAA6B;AAC7B,uCAAwC;AAGxC,iEAAiE;AACjE,4DAA4D;AAC5D,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;AAEtF;;;;GAIG;AACU,QAAA,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;AAElC;;;;GAIG;AACH,SAAgB,gBAAgB,CAAI,QAAqB;IACvD,gEAAgE;IAChE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAAC;AACpC,CAAC;AAHD,4CAGC;AAED;;;;GAIG;AACH,SAAS,OAAO;IACd,4EAA4E;IAC5E,0DAA0D;IAC1D,IAAI,IAAI,GAAQ,EAAE,CAAC;IACnB,IAAI,KAAK,GAAG,IAAW,CAAC;IAExB,KAAK,IAAI,GAAG,IAAI,qBAAW,CAAC,KAAK,CAAC,EAAE;QAClC,IAAI,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;KACnB;IAED,6DAA6D;IAC7D,uEAAuE;IACvE,gEAAgE;IAChE,OAAO,IAAI,CAAC,aAAa,CAAC,CAAC;IAE3B,OAAO,IAAqB,CAAC;AAC/B,CAAC"}
+13
View File
@@ -0,0 +1,13 @@
import { ErrorLike, OnoOptions } from "./types";
/**
* Normalizes Ono options, accounting for defaults and optional options.
*/
export declare function normalizeOptions(options?: OnoOptions): OnoOptions;
/**
* Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
*/
export declare function normalizeArgs<E extends ErrorLike, P extends object>(args: unknown[], options: OnoOptions): {
originalError: E | undefined;
props: P | undefined;
message: string;
};
+59
View File
@@ -0,0 +1,59 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeArgs = exports.normalizeOptions = void 0;
const isomorphic_node_1 = require("./isomorphic.node");
/**
* Normalizes Ono options, accounting for defaults and optional options.
*/
function normalizeOptions(options) {
options = options || {};
return {
concatMessages: options.concatMessages === undefined ? true : Boolean(options.concatMessages),
format: options.format === undefined ? isomorphic_node_1.format
: (typeof options.format === "function" ? options.format : false),
};
}
exports.normalizeOptions = normalizeOptions;
/**
* Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
*/
function normalizeArgs(args, options) {
let originalError;
let props;
let formatArgs;
let message = "";
// Determine which arguments were actually specified
if (typeof args[0] === "string") {
formatArgs = args;
}
else if (typeof args[1] === "string") {
if (args[0] instanceof Error) {
originalError = args[0];
}
else {
props = args[0];
}
formatArgs = args.slice(1);
}
else {
originalError = args[0];
props = args[1];
formatArgs = args.slice(2);
}
// If there are any format arguments, then format the error message
if (formatArgs.length > 0) {
if (options.format) {
message = options.format.apply(undefined, formatArgs);
}
else {
message = formatArgs.join(" ");
}
}
if (options.concatMessages && originalError && originalError.message) {
// The inner-error's message will be added to the new message
message += (message ? " \n" : "") + originalError.message;
}
return { originalError, props, message };
}
exports.normalizeArgs = normalizeArgs;
//# sourceMappingURL=normalize.js.map
@@ -0,0 +1 @@
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":";;;AAAA,uDAA2C;AAG3C;;GAEG;AACH,SAAgB,gBAAgB,CAAC,OAAoB;IACnD,OAAO,GAAG,OAAO,IAAI,EAA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ClB,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;SACvD;aACI;YACH,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SAChC;KACF;IAED,IAAI,OAAO,CAAC,cAAc,IAAI,aAAa,IAAI,aAAa,CAAC,OAAO,EAAE;QACpE,6DAA6D;QAC7D,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC;KAC3D;IAED,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAC3C,CAAC;AAzCD,sCAyCC"}
+3
View File
@@ -0,0 +1,3 @@
import { OnoSingleton } from "./types";
declare const singleton: OnoSingleton;
export { singleton as ono };
+37
View File
@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ono = void 0;
const constructor_1 = require("./constructor");
const singleton = ono;
exports.ono = singleton;
ono.error = new constructor_1.Ono(Error);
ono.eval = new constructor_1.Ono(EvalError);
ono.range = new constructor_1.Ono(RangeError);
ono.reference = new constructor_1.Ono(ReferenceError);
ono.syntax = new constructor_1.Ono(SyntaxError);
ono.type = new constructor_1.Ono(TypeError);
ono.uri = new constructor_1.Ono(URIError);
const onoMap = ono;
/**
* Creates a new error with the specified message, properties, and/or inner error.
* If an inner error is provided, then the new error will match its type, if possible.
*/
function ono(...args) {
let originalError = args[0];
// Is the first argument an Error-like object?
if (typeof originalError === "object" && typeof originalError.name === "string") {
// Try to find an Ono singleton method that matches this error type
for (let typedOno of Object.values(onoMap)) {
if (typeof typedOno === "function" && typedOno.name === "ono") {
let species = typedOno[Symbol.species];
if (species && species !== Error && (originalError instanceof species || originalError.name === species.name)) {
// Create an error of the same type
return typedOno.apply(undefined, args);
}
}
}
}
// By default, create a base Error object
return ono.error.apply(undefined, args);
}
//# sourceMappingURL=single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C,IAAI,CAAC,EAAE;oBAC7G,mCAAmC;oBACnC,OAAO,QAAQ,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;iBACxC;aACF;SACF;KACF;IAED,yCAAyC;IACzC,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAC1C,CAAC"}
+28
View File
@@ -0,0 +1,28 @@
import { ErrorLike } from "./types";
/**
* The Property Descriptor of a lazily-computed `stack` property.
*/
interface LazyStack {
configurable: true;
/**
* Lazily computes the error's stack trace.
*/
get(): string | undefined;
}
/**
* Is the property lazily computed?
*/
export declare function isLazyStack(stackProp: PropertyDescriptor | undefined): stackProp is LazyStack;
/**
* Is the stack property writable?
*/
export declare function isWritableStack(stackProp: PropertyDescriptor | undefined): boolean;
/**
* Appends the original `Error.stack` property to the new Error's stack.
*/
export declare function joinStacks(newError: ErrorLike, originalError?: ErrorLike): string | undefined;
/**
* Calls `joinStacks` lazily, when the `Error.stack` property is accessed.
*/
export declare function lazyJoinStacks(lazyStack: LazyStack, newError: ErrorLike, originalError?: ErrorLike): void;
export {};
+102
View File
@@ -0,0 +1,102 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.lazyJoinStacks = exports.joinStacks = exports.isWritableStack = exports.isLazyStack = void 0;
const newline = /\r?\n/;
const onoCall = /\bono[ @]/;
/**
* Is the property lazily computed?
*/
function isLazyStack(stackProp) {
return Boolean(stackProp &&
stackProp.configurable &&
typeof stackProp.get === "function");
}
exports.isLazyStack = isLazyStack;
/**
* Is the stack property writable?
*/
function isWritableStack(stackProp) {
return Boolean(
// If there is no stack property, then it's writable, since assigning it will create it
!stackProp ||
stackProp.writable ||
typeof stackProp.set === "function");
}
exports.isWritableStack = isWritableStack;
/**
* Appends the original `Error.stack` property to the new Error's stack.
*/
function joinStacks(newError, originalError) {
let newStack = popStack(newError.stack);
let originalStack = originalError ? originalError.stack : undefined;
if (newStack && originalStack) {
return newStack + "\n\n" + originalStack;
}
else {
return newStack || originalStack;
}
}
exports.joinStacks = joinStacks;
/**
* Calls `joinStacks` lazily, when the `Error.stack` property is accessed.
*/
function lazyJoinStacks(lazyStack, newError, originalError) {
if (originalError) {
Object.defineProperty(newError, "stack", {
get: () => {
let newStack = lazyStack.get.apply(newError);
return joinStacks({ stack: newStack }, originalError);
},
enumerable: false,
configurable: true
});
}
else {
lazyPopStack(newError, lazyStack);
}
}
exports.lazyJoinStacks = lazyJoinStacks;
/**
* Removes Ono from the stack, so that the stack starts at the original error location
*/
function popStack(stack) {
if (stack) {
let lines = stack.split(newline);
// Find the Ono call(s) in the stack, and remove them
let onoStart;
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (onoCall.test(line)) {
if (onoStart === undefined) {
// We found the first Ono call in the stack trace.
// There may be other subsequent Ono calls as well.
onoStart = i;
}
}
else if (onoStart !== undefined) {
// We found the first non-Ono call after one or more Ono calls.
// So remove the Ono call lines from the stack trace
lines.splice(onoStart, i - onoStart);
break;
}
}
if (lines.length > 0) {
return lines.join("\n");
}
}
// If we get here, then the stack doesn't contain a call to `ono`.
// This may be due to minification or some optimization of the JS engine.
// So just return the stack as-is.
return stack;
}
/**
* Calls `popStack` lazily, when the `Error.stack` property is accessed.
*/
function lazyPopStack(error, lazyStack) {
Object.defineProperty(error, "stack", {
get: () => popStack(lazyStack.get.apply(error)),
enumerable: false,
configurable: true
});
}
//# sourceMappingURL=stack.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../src/stack.ts"],"names":[],"mappings":";;;AAEA,MAAM,OAAO,GAAG,OAAO,CAAC;AACxB,MAAM,OAAO,GAAG,WAAW,CAAC;AAc5B;;GAEG;AACH,SAAgB,WAAW,CAAC,SAAyC;IACnE,OAAO,OAAO,CACZ,SAAS;QACT,SAAS,CAAC,YAAY;QACtB,OAAO,SAAS,CAAC,GAAG,KAAK,UAAU,CACpC,CAAC;AACJ,CAAC;AAND,kCAMC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jC,qDAAqD;QACrD,IAAI,QAAQ,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACrC,IAAI,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAEpB,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBACtB,IAAI,QAAQ,KAAK,SAAS,EAAE;oBAC1B,kDAAkD;oBAClD,mDAAmD;oBACnD,QAAQ,GAAG,CAAC,CAAC;iBACd;aACF;iBACI,IAAI,QAAQ,KAAK,SAAS,EAAE;gBAC/B,+DAA+D;gBAC/D,oDAAoD;gBACpD,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC;gBACrC,MAAM;aACP;SACF;QAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;YACpB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACzB;KACF;IAED,kEAAkE;IAClE,yEAAyE;IACzE,kCAAkC;IAClC,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,SAAS,YAAY,CAAC,KAAgB,EAAE,SAAoB;IAC1D,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE;QACpC,GAAG,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC/C,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;AACL,CAAC"}
+11
View File
@@ -0,0 +1,11 @@
import { ErrorLike, ErrorPOJO } from "./types";
/**
* Custom JSON serializer for Error objects.
* Returns all built-in error properties, as well as extended properties.
*/
export declare function toJSON<E extends ErrorLike>(this: E): ErrorPOJO & E;
/**
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`.
* Does NOT return members of the base Object prototype, or the specified omitted keys.
*/
export declare function getDeepKeys(obj: object, omit?: Array<string | symbol>): Set<string | symbol>;
+48
View File
@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDeepKeys = exports.toJSON = void 0;
const nonJsonTypes = ["function", "symbol", "undefined"];
const protectedProps = ["constructor", "prototype", "__proto__"];
const objectPrototype = Object.getPrototypeOf({});
/**
* Custom JSON serializer for Error objects.
* Returns all built-in error properties, as well as extended properties.
*/
function toJSON() {
// HACK: We have to cast the objects to `any` so we can use symbol indexers.
// see https://github.com/Microsoft/TypeScript/issues/1863
let pojo = {};
let error = this;
for (let key of getDeepKeys(error)) {
if (typeof key === "string") {
let value = error[key];
let type = typeof value;
if (!nonJsonTypes.includes(type)) {
pojo[key] = value;
}
}
}
return pojo;
}
exports.toJSON = toJSON;
/**
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`.
* Does NOT return members of the base Object prototype, or the specified omitted keys.
*/
function getDeepKeys(obj, omit = []) {
let keys = [];
// Crawl the prototype chain, finding all the string and symbol keys
while (obj && obj !== objectPrototype) {
keys = keys.concat(Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj));
obj = Object.getPrototypeOf(obj);
}
// De-duplicate the list of keys
let uniqueKeys = new Set(keys);
// Remove any omitted keys
for (let key of omit.concat(protectedProps)) {
uniqueKeys.delete(key);
}
return uniqueKeys;
}
exports.getDeepKeys = getDeepKeys;
//# sourceMappingURL=to-json.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"to-json.js","sourceRoot":"","sources":["../src/to-json.ts"],"names":[],"mappings":";;;AAEA,MAAM,YAAY,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AACzD,MAAM,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AACjE,MAAM,eAAe,GAAG,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;AAElD;;;GAGG;AACH,SAAgB,MAAM;IACpB,4EAA4E;IAC5E,0DAA0D;IAC1D,IAAI,IAAI,GAAQ,EAAE,CAAC;IACnB,IAAI,KAAK,GAAG,IAAW,CAAC;IAExB,KAAK,IAAI,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;QAClC,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,IAAI,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;YACvB,IAAI,IAAI,GAAG,OAAO,KAAK,CAAC;YAExB,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;gBAChC,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;aACnB;SACF;KACF;IAED,OAAO,IAAqB,CAAC;AAC/B,CAAC;AAlBD,wBAkBC;AAGD;;;GAGG;AACH,SAAgB,WAAW,CAAC,GAAW,EAAE,OAA+B,EAAE;IACxE,IAAI,IAAI,GAA2B,EAAE,CAAC;IAEtC,oEAAoE;IACpE,OAAO,GAAG,IAAI,GAAG,KAAK,eAAe,EAAE;QACrC,IAAI,GAAG,IAAI,CAAC,MAAM,CAChB,MAAM,CAAC,mBAAmB,CAAC,GAAG,CAAC,EAC/B,MAAM,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAClC,CAAC;QACF,GAAG,GAAG,MAAM,CAAC,cAAc,CAAC,GAAG,CAAW,CAAC;KAC5C;IAED,gCAAgC;IAChC,IAAI,UAAU,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;IAE/B,0BAA0B;IAC1B,KAAK,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE;QAC3C,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KACxB;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AArBD,kCAqBC"}
+201
View File
@@ -0,0 +1,201 @@
/// <reference types="node" />
import { inspect } from "util";
/**
* The default export of the "ono" module.
*/
export interface OnoSingleton extends Ono<Error> {
error: Ono<Error>;
eval: Ono<EvalError>;
range: Ono<RangeError>;
reference: Ono<ReferenceError>;
syntax: Ono<SyntaxError>;
type: Ono<TypeError>;
uri: Ono<URIError>;
}
/**
* Creates an `Ono` instance for a specifc error type.
*/
export interface OnoConstructor {
<T extends ErrorLike>(constructor: ErrorLikeConstructor<T>, options?: OnoOptions): Ono<T>;
new <T extends ErrorLike>(constructor: ErrorLikeConstructor<T>, options?: OnoOptions): Ono<T>;
/**
* Returns an object containing all properties of the given Error object,
* which can be used with `JSON.stringify()`.
*/
toJSON<E extends ErrorLike>(error: E): ErrorPOJO & E;
/**
* Extends the given Error object with enhanced Ono functionality, such as improved support for
* `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
*/
extend<T extends ErrorLike>(error: T): T & OnoError<T>;
/**
* Extends the given Error object with enhanced Ono functionality, such as additional properties
* and improved support for `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
* @param props - An object whose properties will be added to the error
*/
extend<T extends ErrorLike, P extends object>(error: T, props?: P): T & P & OnoError<T & P>;
/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces
* and improved support for `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
* @param originalError - The original error. This error's stack trace will be added to the error's stack trace.
*/
extend<T extends ErrorLike, E extends ErrorLike>(error: T, originalError?: E): T & E & OnoError<T & E>;
/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
* additional properties, and improved support for `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
* @param originalError - The original error. This error's stack trace will be added to the error's stack trace.
* @param props - An object whose properties will be added to the error
*/
extend<T extends ErrorLike, E extends ErrorLike, P extends object>(error: T, originalError?: E, props?: P): T & E & P & OnoError<T & E & P>;
}
/**
* An `Ono` is a function that creates errors of a specific type.
*/
export interface Ono<T extends ErrorLike> {
/**
* The type of Error that this `Ono` function produces.
*/
readonly [Symbol.species]: ErrorLikeConstructor<T>;
/**
* Creates a new error with the message, stack trace, and properties of another error.
*
* @param error - The original error
*/
<E extends ErrorLike>(error: E): T & E & OnoError<T & E>;
/**
* Creates a new error with the message, stack trace, and properties of another error,
* as well as aditional properties.
*
* @param error - The original error
* @param props - An object whose properties will be added to the returned error
*/
<E extends ErrorLike, P extends object>(error: E, props: P): T & E & P & OnoError<T & E & P>;
/**
* Creates a new error with a formatted message and the stack trace and properties of another error.
*
* @param error - The original error
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<E extends ErrorLike>(error: E, message: string, ...params: unknown[]): T & E & OnoError<T & E>;
/**
* Creates a new error with a formatted message and the stack trace and properties of another error,
* as well as additional properties.
*
* @param error - The original error
* @param props - An object whose properties will be added to the returned error
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<E extends ErrorLike, P extends object>(error: E, props: P, message: string, ...params: unknown[]): T & E & P & OnoError<T & E & P>;
/**
* Creates an error with a formatted message.
*
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
(message: string, ...params: unknown[]): T & OnoError<T>;
/**
* Creates an error with additional properties.
*
* @param props - An object whose properties will be added to the returned error
*/
<P extends object>(props: P): T & P & OnoError<T & P>;
/**
* Creates an error with a formatted message and additional properties.
*
* @param props - An object whose properties will be added to the returned error
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<P extends object>(props: P, message: string, ...params: unknown[]): T & P & OnoError<T & P>;
}
/**
* All error objects returned by Ono have these properties.
*/
export interface OnoError<T> extends ErrorPOJO {
/**
* Returns a JSON representation of the error, including all built-in error properties,
* as well as properties that were dynamically added.
*/
toJSON(): ErrorPOJO & T;
/**
* Returns a representation of the error for Node's `util.inspect()` method.
*
* @see https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
*/
[inspect.custom](): ErrorPOJO & T;
}
/**
* An error object that doesn't inherit from the `Error` class, such as `DOMError`, `DOMException`,
* and some third-party error types.
*/
export interface ErrorPOJO {
message?: string;
stack?: string;
name?: string;
}
/**
* Any object that "looks like" an `Error` object.
*/
export declare type ErrorLike = Error | ErrorPOJO;
/**
* A constructor for `ErrorLike` objects.
*/
export declare type ErrorLikeConstructor<T extends ErrorLike> = ErrorLikeConstructorFunction<T> | ErrorLikeConstructorClass<T>;
/**
* A constructor function for `ErrorLike` objects.
* Constructor functions can be called without the `new` keyword.
*
* @example
* throw TypeError();
*/
export interface ErrorLikeConstructorFunction<T extends ErrorLike> {
readonly prototype: T;
(): T;
}
/**
* A constructor class for `ErrorLike` objects.
* Constructor classes must be called with the `new` keyword.
*
* @example
* throw new TypeError();
*/
export interface ErrorLikeConstructorClass<T extends ErrorLike> {
readonly prototype: T;
new (...args: unknown[]): T;
}
/**
* Options that determine the behavior of an `Ono` instance.
*/
export interface OnoOptions {
/**
* When `Ono` is used to wrap an error, this setting determines whether the inner error's message
* is appended to the new error message.
*
* Defaults to `true`.
*/
concatMessages?: boolean;
/**
* A function that replaces placeholders like "%s" or "%d" in error messages with values.
* If set to `false`, then error messages will be treated as literals and no placeholder replacement will occur.
*
* Defaults to `utils.inspect()` in Node.js. Defaults to `Array.join()` in browsers.
*/
format?: MessageFormatter | false;
}
/**
* A function that accepts a message template and arguments to replace template parameters.
*
* @example
* format("Hello, %s! You have %d unread messages.", "John", 5);
*/
export declare type MessageFormatter = (message: string, ...args: unknown[]) => string;
+4
View File
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
//# sourceMappingURL=types.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;AAAA,+BAA+B"}
@@ -0,0 +1,3 @@
import { OnoConstructor } from "./types";
declare const constructor: OnoConstructor;
export { constructor as Ono };
+44
View File
@@ -0,0 +1,44 @@
import { extendError } from "./extend-error";
import { normalizeArgs, normalizeOptions } from "./normalize";
import { toJSON as errorToJSON } from "./to-json";
const constructor = Ono;
export { constructor as Ono };
/**
* Creates an `Ono` instance for a specifc error type.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
function Ono(ErrorConstructor, options) {
options = normalizeOptions(options);
function ono(...args) {
let { originalError, props, message } = normalizeArgs(args, options);
// Create a new error of the specified type
let newError = new ErrorConstructor(message);
// Extend the error with the properties of the original error and the `props` object
return extendError(newError, originalError, props);
}
ono[Symbol.species] = ErrorConstructor;
return ono;
}
/**
* Returns an object containing all properties of the given Error object,
* which can be used with `JSON.stringify()`.
*/
Ono.toJSON = function toJSON(error) {
return errorToJSON.call(error);
};
/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
* additional properties, and improved support for `JSON.stringify()`.
*/
Ono.extend = function extend(error, originalError, props) {
if (props || originalError instanceof Error) {
return extendError(error, originalError, props);
}
else if (originalError) {
return extendError(error, undefined, originalError);
}
else {
return extendError(error);
}
};
//# sourceMappingURL=constructor.js.map
@@ -0,0 +1 @@
{"version":3,"file":"constructor.js","sourceRoot":"","sources":["../src/constructor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE,MAAM,WAAW,CAAC;AAGlD,MAAM,WAAW,GAAG,GAAqB,CAAC;AAC1C,OAAO,EAAE,WAAW,IAAI,GAAG,EAAE,CAAC;AAE9B;;GAEG;AACH,gEAAgE;AAChE,SAAS,GAAG,CAAsB,gBAAyC,EAAE,OAAoB;IAC/F,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAEpC,SAAS,GAAG,CAAwC,GAAG,IAAe;QACpE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,aAAa,CAAO,IAAI,EAAE,OAAQ,CAAC,CAAC;QAE5E,2CAA2C;QAC3C,IAAI,QAAQ,GAAG,IAAK,gBAAiD,CAAC,OAAO,CAAC,CAAC;QAE/E,oFAAoF;QACpF,OAAO,WAAW,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;IACrD,CAAC;IAED,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,gBAAgB,CAAC;IACvC,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,GAAG,CAAC,MAAM,GAAG,SAAS,MAAM,CAAC,KAAgB;IAC3C,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF;;;GAGG;AACH,GAAG,CAAC,MAAM,GAAG,SAAS,MAAM,CAAC,KAAgB,EAAE,aAAyB,EAAE,KAAc;IACtF,IAAI,KAAK,IAAI,aAAa,YAAY,KAAK,EAAE;QAC3C,OAAO,WAAW,CAAC,KAAK,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;KACjD;SACI,IAAI,aAAa,EAAE;QACtB,OAAO,WAAW,CAAC,KAAK,EAAE,SAAS,EAAE,aAAa,CAAC,CAAC;KACrD;SACI;QACH,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC;KAC3B;AACH,CAAC,CAAC"}
@@ -0,0 +1,9 @@
import { ErrorLike, OnoError } from "./types";
/**
* Extends the new error with the properties of the original error and the `props` object.
*
* @param newError - The error object to extend
* @param originalError - The original error object, if any
* @param props - Additional properties to add, if any
*/
export declare function extendError<T extends ErrorLike, E extends ErrorLike, P extends object>(error: T, originalError?: E, props?: P): T & E & P & OnoError<T & E & P>;
+73
View File
@@ -0,0 +1,73 @@
import { addInspectMethod } from "./isomorphic.node";
import { isLazyStack, isWritableStack, joinStacks, lazyJoinStacks } from "./stack";
import { getDeepKeys, toJSON } from "./to-json";
const protectedProps = ["name", "message", "stack"];
/**
* Extends the new error with the properties of the original error and the `props` object.
*
* @param newError - The error object to extend
* @param originalError - The original error object, if any
* @param props - Additional properties to add, if any
*/
export function extendError(error, originalError, props) {
let onoError = error;
extendStack(onoError, originalError);
// Copy properties from the original error
if (originalError && typeof originalError === "object") {
mergeErrors(onoError, originalError);
}
// The default `toJSON` method doesn't output props like `name`, `message`, `stack`, etc.
// So replace it with one that outputs every property of the error.
onoError.toJSON = toJSON;
// On Node.js, add support for the `util.inspect()` method
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (addInspectMethod) {
addInspectMethod(onoError);
}
// Finally, copy custom properties that were specified by the user.
// These props OVERWRITE any previous props
if (props && typeof props === "object") {
Object.assign(onoError, props);
}
return onoError;
}
/**
* Extend the error stack to include its cause
*/
function extendStack(newError, originalError) {
let stackProp = Object.getOwnPropertyDescriptor(newError, "stack");
if (isLazyStack(stackProp)) {
lazyJoinStacks(stackProp, newError, originalError);
}
else if (isWritableStack(stackProp)) {
newError.stack = joinStacks(newError, originalError);
}
}
/**
* Merges properties of the original error with the new error.
*
* @param newError - The error object to extend
* @param originalError - The original error object, if any
*/
function mergeErrors(newError, originalError) {
// Get the original error's keys
// NOTE: We specifically exclude properties that we have already set on the new error.
// This is _especially_ important for the `stack` property, because this property has
// a lazy getter in some environments
let keys = getDeepKeys(originalError, protectedProps);
// HACK: We have to cast the errors to `any` so we can use symbol indexers.
// see https://github.com/Microsoft/TypeScript/issues/1863
let _newError = newError;
let _originalError = originalError;
for (let key of keys) {
if (_newError[key] === undefined) {
try {
_newError[key] = _originalError[key];
}
catch (e) {
// This property is read-only, so it can't be copied
}
}
}
}
//# sourceMappingURL=extend-error.js.map
@@ -0,0 +1 @@
{"version":3,"file":"extend-error.js","sourceRoot":"","sources":["../src/extend-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACnF,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE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FAAqF;IACrF,qCAAqC;IACrC,IAAI,IAAI,GAAG,WAAW,CAAC,aAAa,EAAE,cAAc,CAAC,CAAC;IAEtD,2EAA2E;IAC3E,0DAA0D;IAC1D,IAAI,SAAS,GAAG,QAAe,CAAC;IAChC,IAAI,cAAc,GAAG,aAAoB,CAAC;IAE1C,KAAK,IAAI,GAAG,IAAI,IAAI,EAAE;QACpB,IAAI,SAAS,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;YAChC,IAAI;gBACF,SAAS,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;aACtC;YACD,OAAO,CAAC,EAAE;gBACR,oDAAoD;aACrD;SACF;KACF;AACH,CAAC"}
+5
View File
@@ -0,0 +1,5 @@
import { ono } from "./singleton";
export { Ono } from "./constructor";
export * from "./types";
export { ono };
export default ono;
+11
View File
@@ -0,0 +1,11 @@
/* eslint-env commonjs */
import { ono } from "./singleton";
export { Ono } from "./constructor";
export * from "./types";
export { ono };
export default ono;
// CommonJS default export hack
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = Object.assign(module.exports.default, module.exports);
}
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,GAAG,EAAE,CAAC;AAEf,eAAe,GAAG,CAAC;AAEnB,+BAA+B;AAC/B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,EAAE;IACpE,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;CACxE"}
@@ -0,0 +1,15 @@
/**
* Ono supports custom formatters for error messages. In Node.js, it defaults
* to the `util.format()` function. In browsers, it defaults to `Array.join()`.
*
* The Node.js functionality can be used in a web browser via a polyfill,
* such as "format-util".
*
* @see https://github.com/tmpfs/format-util
*/
export declare const format = false;
/**
* The `util.inspect()` functionality only applies to Node.js.
* We return the constant `false` here so that the Node-specific code gets removed by tree-shaking.
*/
export declare const addInspectMethod = false;
@@ -0,0 +1,16 @@
/**
* Ono supports custom formatters for error messages. In Node.js, it defaults
* to the `util.format()` function. In browsers, it defaults to `Array.join()`.
*
* The Node.js functionality can be used in a web browser via a polyfill,
* such as "format-util".
*
* @see https://github.com/tmpfs/format-util
*/
export const format = false;
/**
* The `util.inspect()` functionality only applies to Node.js.
* We return the constant `false` here so that the Node-specific code gets removed by tree-shaking.
*/
export const addInspectMethod = false;
//# sourceMappingURL=isomorphic.browser.js.map
@@ -0,0 +1 @@
{"version":3,"file":"isomorphic.browser.js","sourceRoot":"","sources":["../src/isomorphic.browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,KAAK,CAAC;AAE5B;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC"}
@@ -0,0 +1,15 @@
/// <reference types="node" />
import * as util from "util";
import { OnoError } from "./types";
/**
* Ono supports Node's `util.format()` formatting for error messages.
*
* @see https://nodejs.org/api/util.html#util_util_format_format_args
*/
export declare const format: typeof util.format;
/**
* Adds an `inspect()` method to support Node's `util.inspect()` function.
*
* @see https://nodejs.org/api/util.html#util_util_inspect_custom
*/
export declare function addInspectMethod<T>(newError: OnoError<T>): void;
@@ -0,0 +1,41 @@
import * as util from "util";
import { getDeepKeys } from "./to-json";
// The `inspect()` method is actually a Symbol, not a string key.
// https://nodejs.org/api/util.html#util_util_inspect_custom
const inspectMethod = util.inspect.custom || Symbol.for("nodejs.util.inspect.custom");
/**
* Ono supports Node's `util.format()` formatting for error messages.
*
* @see https://nodejs.org/api/util.html#util_util_format_format_args
*/
export const format = util.format;
/**
* Adds an `inspect()` method to support Node's `util.inspect()` function.
*
* @see https://nodejs.org/api/util.html#util_util_inspect_custom
*/
export function addInspectMethod(newError) {
// @ts-expect-error - TypeScript doesn't support symbol indexers
newError[inspectMethod] = inspect;
}
/**
* Returns a representation of the error for Node's `util.inspect()` method.
*
* @see https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
*/
function inspect() {
// HACK: We have to cast the objects to `any` so we can use symbol indexers.
// see https://github.com/Microsoft/TypeScript/issues/1863
let pojo = {};
let error = this;
for (let key of getDeepKeys(error)) {
let value = error[key];
pojo[key] = value;
}
// Don't include the `inspect()` method on the output object,
// otherwise it will cause `util.inspect()` to go into an infinite loop
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete pojo[inspectMethod];
return pojo;
}
//# sourceMappingURL=isomorphic.node.js.map
@@ -0,0 +1 @@
{"version":3,"file":"isomorphic.node.js","sourceRoot":"","sources":["../src/isomorphic.node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxC,iEAAiE;AACjE,4DAA4D;AAC5D,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;AAEtF;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;AAElC;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAI,QAAqB;IACvD,gEAAgE;IAChE,QAAQ,CAAC,aAAa,CAAC,GAAG,OAAO,CAAC;AACpC,CAAC;AAED;;;;GAIG;AACH,SAAS,OAAO;IACd,4EAA4E;IAC5E,0DAA0D;IAC1D,IAAI,IAAI,GAAQ,EAAE,CAAC;IACnB,IAAI,KAAK,GAAG,IAAW,CAAC;IAExB,KAAK,IAAI,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;QAClC,IAAI,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;KACnB;IAED,6DAA6D;IAC7D,uEAAuE;IACvE,gEAAgE;IAChE,OAAO,IAAI,CAAC,aAAa,CAAC,CAAC;IAE3B,OAAO,IAAqB,CAAC;AAC/B,CAAC"}
+13
View File
@@ -0,0 +1,13 @@
import { ErrorLike, OnoOptions } from "./types";
/**
* Normalizes Ono options, accounting for defaults and optional options.
*/
export declare function normalizeOptions(options?: OnoOptions): OnoOptions;
/**
* Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
*/
export declare function normalizeArgs<E extends ErrorLike, P extends object>(args: unknown[], options: OnoOptions): {
originalError: E | undefined;
props: P | undefined;
message: string;
};
+54
View File
@@ -0,0 +1,54 @@
import { format } from "./isomorphic.node";
/**
* Normalizes Ono options, accounting for defaults and optional options.
*/
export function normalizeOptions(options) {
options = options || {};
return {
concatMessages: options.concatMessages === undefined ? true : Boolean(options.concatMessages),
format: options.format === undefined ? format
: (typeof options.format === "function" ? options.format : false),
};
}
/**
* Normalizes the Ono arguments, accounting for defaults, options, and optional arguments.
*/
export function normalizeArgs(args, options) {
let originalError;
let props;
let formatArgs;
let message = "";
// Determine which arguments were actually specified
if (typeof args[0] === "string") {
formatArgs = args;
}
else if (typeof args[1] === "string") {
if (args[0] instanceof Error) {
originalError = args[0];
}
else {
props = args[0];
}
formatArgs = args.slice(1);
}
else {
originalError = args[0];
props = args[1];
formatArgs = args.slice(2);
}
// If there are any format arguments, then format the error message
if (formatArgs.length > 0) {
if (options.format) {
message = options.format.apply(undefined, formatArgs);
}
else {
message = formatArgs.join(" ");
}
}
if (options.concatMessages && originalError && originalError.message) {
// The inner-error's message will be added to the new message
message += (message ? " \n" : "") + originalError.message;
}
return { originalError, props, message };
}
//# sourceMappingURL=normalize.js.map
@@ -0,0 +1 @@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AAC,CAAC,CAAM,CAAC;QACrB,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5B;IAED,mEAAmE;IACnE,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;QACzB,IAAI,OAAO,CAAC,MAAM,EAAE;YAClB,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;SACvD;aACI;YACH,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SAChC;KACF;IAED,IAAI,OAAO,CAAC,cAAc,IAAI,aAAa,IAAI,aAAa,CAAC,OAAO,EAAE;QACpE,6DAA6D;QAC7D,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC;KAC3D;IAED,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAC3C,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
import { OnoSingleton } from "./types";
declare const singleton: OnoSingleton;
export { singleton as ono };
+34
View File
@@ -0,0 +1,34 @@
import { Ono as OnoConstructor } from "./constructor";
const singleton = ono;
export { singleton as ono };
ono.error = new OnoConstructor(Error);
ono.eval = new OnoConstructor(EvalError);
ono.range = new OnoConstructor(RangeError);
ono.reference = new OnoConstructor(ReferenceError);
ono.syntax = new OnoConstructor(SyntaxError);
ono.type = new OnoConstructor(TypeError);
ono.uri = new OnoConstructor(URIError);
const onoMap = ono;
/**
* Creates a new error with the specified message, properties, and/or inner error.
* If an inner error is provided, then the new error will match its type, if possible.
*/
function ono(...args) {
let originalError = args[0];
// Is the first argument an Error-like object?
if (typeof originalError === "object" && typeof originalError.name === "string") {
// Try to find an Ono singleton method that matches this error type
for (let typedOno of Object.values(onoMap)) {
if (typeof typedOno === "function" && typedOno.name === "ono") {
let species = typedOno[Symbol.species];
if (species && species !== Error && (originalError instanceof species || originalError.name === species.name)) {
// Create an error of the same type
return typedOno.apply(undefined, args);
}
}
}
}
// By default, create a base Error object
return ono.error.apply(undefined, args);
}
//# sourceMappingURL=singleton.js.map
@@ -0,0 +1 @@
{"version":3,"file":"singleton.js","sourceRoot":"","sources":["../src/singleton.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,IAAI,cAAc,EAAE,MAAM,eAAe,CAAC;AAGtD,MAAM,SAAS,GAAG,GAAmB,CAAC;AACtC,OAAO,EAAE,SAAS,IAAI,GAAG,EAAE,CAAC;AAE5B,GAAG,CAAC,KAAK,GAAG,IAAI,cAAc,CAAC,KAAK,CAAC,CAAC;AACtC,GAAG,CAAC,IAAI,GAAG,IAAI,cAAc,CAAC,SAAS,CAAC,CAAC;AACzC,GAAG,CAAC,KAAK,GAAG,IAAI,cAAc,CAAC,UAAU,CAAC,CAAC;AAC3C,GAAG,CAAC,SAAS,GAAG,IAAI,cAAc,CAAC,cAAc,CAAC,CAAC;AACnD,GAAG,CAAC,MAAM,GAAG,IAAI,cAAc,CAAC,WAAW,CAAC,CAAC;AAC7C,GAAG,CAAC,IAAI,GAAG,IAAI,cAAc,CAAC,SAAS,CAAC,CAAC;AACzC,GAAG,CAAC,GAAG,GAAG,IAAI,cAAc,CAAC,QAAQ,CAAC,CAAC;AAEvC,MAAM,MAAM,GAAG,GAA4C,CAAC;AAE5D;;;GAGG;AACH,SAAS,GAAG,CAAwC,GAAG,IAAe;IACpE,IAAI,aAAa,GAAG,IAAI,CAAC,CAAC,CAA0B,CAAC;IAErD,8CAA8C;IAC9C,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,OAAO,aAAa,CAAC,IAAI,KAAK,QAAQ,EAAE;QAE/E,mEAAmE;QACnE,KAAK,IAAI,QAAQ,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;YAC1C,IAAI,OAAO,QAAQ,KAAK,UAAU,IAAI,QAAQ,CAAC,IAAI,KAAK,KAAK,EAAE;gBAC7D,IAAI,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEvC,IAAI,OAAO,IAAI,OAAO,KAAK,KAAK,IAAI,CAAC,aAAa,YAAY,OAAO,IAAI,aAAa,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,EAAE;oBAC7G,mCAAmC;oBACnC,OAAO,QAAQ,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;iBACxC;aACF;SACF;KACF;IAED,yCAAyC;IACzC,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAC1C,CAAC"}
+28
View File
@@ -0,0 +1,28 @@
import { ErrorLike } from "./types";
/**
* The Property Descriptor of a lazily-computed `stack` property.
*/
interface LazyStack {
configurable: true;
/**
* Lazily computes the error's stack trace.
*/
get(): string | undefined;
}
/**
* Is the property lazily computed?
*/
export declare function isLazyStack(stackProp: PropertyDescriptor | undefined): stackProp is LazyStack;
/**
* Is the stack property writable?
*/
export declare function isWritableStack(stackProp: PropertyDescriptor | undefined): boolean;
/**
* Appends the original `Error.stack` property to the new Error's stack.
*/
export declare function joinStacks(newError: ErrorLike, originalError?: ErrorLike): string | undefined;
/**
* Calls `joinStacks` lazily, when the `Error.stack` property is accessed.
*/
export declare function lazyJoinStacks(lazyStack: LazyStack, newError: ErrorLike, originalError?: ErrorLike): void;
export {};
+95
View File
@@ -0,0 +1,95 @@
const newline = /\r?\n/;
const onoCall = /\bono[ @]/;
/**
* Is the property lazily computed?
*/
export function isLazyStack(stackProp) {
return Boolean(stackProp &&
stackProp.configurable &&
typeof stackProp.get === "function");
}
/**
* Is the stack property writable?
*/
export function isWritableStack(stackProp) {
return Boolean(
// If there is no stack property, then it's writable, since assigning it will create it
!stackProp ||
stackProp.writable ||
typeof stackProp.set === "function");
}
/**
* Appends the original `Error.stack` property to the new Error's stack.
*/
export function joinStacks(newError, originalError) {
let newStack = popStack(newError.stack);
let originalStack = originalError ? originalError.stack : undefined;
if (newStack && originalStack) {
return newStack + "\n\n" + originalStack;
}
else {
return newStack || originalStack;
}
}
/**
* Calls `joinStacks` lazily, when the `Error.stack` property is accessed.
*/
export function lazyJoinStacks(lazyStack, newError, originalError) {
if (originalError) {
Object.defineProperty(newError, "stack", {
get: () => {
let newStack = lazyStack.get.apply(newError);
return joinStacks({ stack: newStack }, originalError);
},
enumerable: false,
configurable: true
});
}
else {
lazyPopStack(newError, lazyStack);
}
}
/**
* Removes Ono from the stack, so that the stack starts at the original error location
*/
function popStack(stack) {
if (stack) {
let lines = stack.split(newline);
// Find the Ono call(s) in the stack, and remove them
let onoStart;
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (onoCall.test(line)) {
if (onoStart === undefined) {
// We found the first Ono call in the stack trace.
// There may be other subsequent Ono calls as well.
onoStart = i;
}
}
else if (onoStart !== undefined) {
// We found the first non-Ono call after one or more Ono calls.
// So remove the Ono call lines from the stack trace
lines.splice(onoStart, i - onoStart);
break;
}
}
if (lines.length > 0) {
return lines.join("\n");
}
}
// If we get here, then the stack doesn't contain a call to `ono`.
// This may be due to minification or some optimization of the JS engine.
// So just return the stack as-is.
return stack;
}
/**
* Calls `popStack` lazily, when the `Error.stack` property is accessed.
*/
function lazyPopStack(error, lazyStack) {
Object.defineProperty(error, "stack", {
get: () => popStack(lazyStack.get.apply(error)),
enumerable: false,
configurable: true
});
}
//# sourceMappingURL=stack.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../src/stack.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,GAAG,OAAO,CAAC;AACxB,MAAM,OAAO,GAAG,WAAW,CAAC;AAc5B;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,SAAyC;IACnE,OAAO,OAAO,CACZ,SAAS;QACT,SAAS,CAAC,YAAY;QACtB,OAAO,SAAS,CAAC,GAAG,KAAK,UAAU,CACpC,CAAC;AACJ,CAAC;AAED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,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACrC,IAAI,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAEpB,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBACtB,IAAI,QAAQ,KAAK,SAAS,EAAE;oBAC1B,kDAAkD;oBAClD,mDAAmD;oBACnD,QAAQ,GAAG,CAAC,CAAC;iBACd;aACF;iBACI,IAAI,QAAQ,KAAK,SAAS,EAAE;gBAC/B,+DAA+D;gBAC/D,oDAAoD;gBACpD,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC;gBACrC,MAAM;aACP;SACF;QAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;YACpB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACzB;KACF;IAED,kEAAkE;IAClE,yEAAyE;IACzE,kCAAkC;IAClC,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,SAAS,YAAY,CAAC,KAAgB,EAAE,SAAoB;IAC1D,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE;QACpC,GAAG,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC/C,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;AACL,CAAC"}
+11
View File
@@ -0,0 +1,11 @@
import { ErrorLike, ErrorPOJO } from "./types";
/**
* Custom JSON serializer for Error objects.
* Returns all built-in error properties, as well as extended properties.
*/
export declare function toJSON<E extends ErrorLike>(this: E): ErrorPOJO & E;
/**
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`.
* Does NOT return members of the base Object prototype, or the specified omitted keys.
*/
export declare function getDeepKeys(obj: object, omit?: Array<string | symbol>): Set<string | symbol>;
+43
View File
@@ -0,0 +1,43 @@
const nonJsonTypes = ["function", "symbol", "undefined"];
const protectedProps = ["constructor", "prototype", "__proto__"];
const objectPrototype = Object.getPrototypeOf({});
/**
* Custom JSON serializer for Error objects.
* Returns all built-in error properties, as well as extended properties.
*/
export function toJSON() {
// HACK: We have to cast the objects to `any` so we can use symbol indexers.
// see https://github.com/Microsoft/TypeScript/issues/1863
let pojo = {};
let error = this;
for (let key of getDeepKeys(error)) {
if (typeof key === "string") {
let value = error[key];
let type = typeof value;
if (!nonJsonTypes.includes(type)) {
pojo[key] = value;
}
}
}
return pojo;
}
/**
* Returns own, inherited, enumerable, non-enumerable, string, and symbol keys of `obj`.
* Does NOT return members of the base Object prototype, or the specified omitted keys.
*/
export function getDeepKeys(obj, omit = []) {
let keys = [];
// Crawl the prototype chain, finding all the string and symbol keys
while (obj && obj !== objectPrototype) {
keys = keys.concat(Object.getOwnPropertyNames(obj), Object.getOwnPropertySymbols(obj));
obj = Object.getPrototypeOf(obj);
}
// De-duplicate the list of keys
let uniqueKeys = new Set(keys);
// Remove any omitted keys
for (let key of omit.concat(protectedProps)) {
uniqueKeys.delete(key);
}
return uniqueKeys;
}
//# sourceMappingURL=to-json.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"to-json.js","sourceRoot":"","sources":["../src/to-json.ts"],"names":[],"mappings":"AAEA,MAAM,YAAY,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AACzD,MAAM,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AACjE,MAAM,eAAe,GAAG,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;AAElD;;;GAGG;AACH,MAAM,UAAU,MAAM;IACpB,4EAA4E;IAC5E,0DAA0D;IAC1D,IAAI,IAAI,GAAQ,EAAE,CAAC;IACnB,IAAI,KAAK,GAAG,IAAW,CAAC;IAExB,KAAK,IAAI,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;QAClC,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,IAAI,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;YACvB,IAAI,IAAI,GAAG,OAAO,KAAK,CAAC;YAExB,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;gBAChC,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;aACnB;SACF;KACF;IAED,OAAO,IAAqB,CAAC;AAC/B,CAAC;AAGD;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW,EAAE,OAA+B,EAAE;IACxE,IAAI,IAAI,GAA2B,EAAE,CAAC;IAEtC,oEAAoE;IACpE,OAAO,GAAG,IAAI,GAAG,KAAK,eAAe,EAAE;QACrC,IAAI,GAAG,IAAI,CAAC,MAAM,CAChB,MAAM,CAAC,mBAAmB,CAAC,GAAG,CAAC,EAC/B,MAAM,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAClC,CAAC;QACF,GAAG,GAAG,MAAM,CAAC,cAAc,CAAC,GAAG,CAAW,CAAC;KAC5C;IAED,gCAAgC;IAChC,IAAI,UAAU,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC;IAE/B,0BAA0B;IAC1B,KAAK,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE;QAC3C,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KACxB;IAED,OAAO,UAAU,CAAC;AACpB,CAAC"}
+201
View File
@@ -0,0 +1,201 @@
/// <reference types="node" />
import { inspect } from "util";
/**
* The default export of the "ono" module.
*/
export interface OnoSingleton extends Ono<Error> {
error: Ono<Error>;
eval: Ono<EvalError>;
range: Ono<RangeError>;
reference: Ono<ReferenceError>;
syntax: Ono<SyntaxError>;
type: Ono<TypeError>;
uri: Ono<URIError>;
}
/**
* Creates an `Ono` instance for a specifc error type.
*/
export interface OnoConstructor {
<T extends ErrorLike>(constructor: ErrorLikeConstructor<T>, options?: OnoOptions): Ono<T>;
new <T extends ErrorLike>(constructor: ErrorLikeConstructor<T>, options?: OnoOptions): Ono<T>;
/**
* Returns an object containing all properties of the given Error object,
* which can be used with `JSON.stringify()`.
*/
toJSON<E extends ErrorLike>(error: E): ErrorPOJO & E;
/**
* Extends the given Error object with enhanced Ono functionality, such as improved support for
* `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
*/
extend<T extends ErrorLike>(error: T): T & OnoError<T>;
/**
* Extends the given Error object with enhanced Ono functionality, such as additional properties
* and improved support for `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
* @param props - An object whose properties will be added to the error
*/
extend<T extends ErrorLike, P extends object>(error: T, props?: P): T & P & OnoError<T & P>;
/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces
* and improved support for `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
* @param originalError - The original error. This error's stack trace will be added to the error's stack trace.
*/
extend<T extends ErrorLike, E extends ErrorLike>(error: T, originalError?: E): T & E & OnoError<T & E>;
/**
* Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
* additional properties, and improved support for `JSON.stringify()`.
*
* @param error - The error object to extend. This object instance will be modified and returned.
* @param originalError - The original error. This error's stack trace will be added to the error's stack trace.
* @param props - An object whose properties will be added to the error
*/
extend<T extends ErrorLike, E extends ErrorLike, P extends object>(error: T, originalError?: E, props?: P): T & E & P & OnoError<T & E & P>;
}
/**
* An `Ono` is a function that creates errors of a specific type.
*/
export interface Ono<T extends ErrorLike> {
/**
* The type of Error that this `Ono` function produces.
*/
readonly [Symbol.species]: ErrorLikeConstructor<T>;
/**
* Creates a new error with the message, stack trace, and properties of another error.
*
* @param error - The original error
*/
<E extends ErrorLike>(error: E): T & E & OnoError<T & E>;
/**
* Creates a new error with the message, stack trace, and properties of another error,
* as well as aditional properties.
*
* @param error - The original error
* @param props - An object whose properties will be added to the returned error
*/
<E extends ErrorLike, P extends object>(error: E, props: P): T & E & P & OnoError<T & E & P>;
/**
* Creates a new error with a formatted message and the stack trace and properties of another error.
*
* @param error - The original error
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<E extends ErrorLike>(error: E, message: string, ...params: unknown[]): T & E & OnoError<T & E>;
/**
* Creates a new error with a formatted message and the stack trace and properties of another error,
* as well as additional properties.
*
* @param error - The original error
* @param props - An object whose properties will be added to the returned error
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<E extends ErrorLike, P extends object>(error: E, props: P, message: string, ...params: unknown[]): T & E & P & OnoError<T & E & P>;
/**
* Creates an error with a formatted message.
*
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
(message: string, ...params: unknown[]): T & OnoError<T>;
/**
* Creates an error with additional properties.
*
* @param props - An object whose properties will be added to the returned error
*/
<P extends object>(props: P): T & P & OnoError<T & P>;
/**
* Creates an error with a formatted message and additional properties.
*
* @param props - An object whose properties will be added to the returned error
* @param message - The new error message, possibly including argument placeholders
* @param params - Optional arguments to replace the corresponding placeholders in the message
*/
<P extends object>(props: P, message: string, ...params: unknown[]): T & P & OnoError<T & P>;
}
/**
* All error objects returned by Ono have these properties.
*/
export interface OnoError<T> extends ErrorPOJO {
/**
* Returns a JSON representation of the error, including all built-in error properties,
* as well as properties that were dynamically added.
*/
toJSON(): ErrorPOJO & T;
/**
* Returns a representation of the error for Node's `util.inspect()` method.
*
* @see https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects
*/
[inspect.custom](): ErrorPOJO & T;
}
/**
* An error object that doesn't inherit from the `Error` class, such as `DOMError`, `DOMException`,
* and some third-party error types.
*/
export interface ErrorPOJO {
message?: string;
stack?: string;
name?: string;
}
/**
* Any object that "looks like" an `Error` object.
*/
export declare type ErrorLike = Error | ErrorPOJO;
/**
* A constructor for `ErrorLike` objects.
*/
export declare type ErrorLikeConstructor<T extends ErrorLike> = ErrorLikeConstructorFunction<T> | ErrorLikeConstructorClass<T>;
/**
* A constructor function for `ErrorLike` objects.
* Constructor functions can be called without the `new` keyword.
*
* @example
* throw TypeError();
*/
export interface ErrorLikeConstructorFunction<T extends ErrorLike> {
readonly prototype: T;
(): T;
}
/**
* A constructor class for `ErrorLike` objects.
* Constructor classes must be called with the `new` keyword.
*
* @example
* throw new TypeError();
*/
export interface ErrorLikeConstructorClass<T extends ErrorLike> {
readonly prototype: T;
new (...args: unknown[]): T;
}
/**
* Options that determine the behavior of an `Ono` instance.
*/
export interface OnoOptions {
/**
* When `Ono` is used to wrap an error, this setting determines whether the inner error's message
* is appended to the new error message.
*
* Defaults to `true`.
*/
concatMessages?: boolean;
/**
* A function that replaces placeholders like "%s" or "%d" in error messages with values.
* If set to `false`, then error messages will be treated as literals and no placeholder replacement will occur.
*
* Defaults to `utils.inspect()` in Node.js. Defaults to `Array.join()` in browsers.
*/
format?: MessageFormatter | false;
}
/**
* A function that accepts a message template and arguments to replace template parameters.
*
* @example
* format("Hello, %s! You have %d unread messages.", "John", 5);
*/
export declare type MessageFormatter = (message: string, ...args: unknown[]) => string;
+2
View File
@@ -0,0 +1,2 @@
import { inspect } from "util";
//# sourceMappingURL=types.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC"}
+74
View File
@@ -0,0 +1,74 @@
{
"name": "@jsdevtools/ono",
"version": "7.1.3",
"description": "Throw better errors.",
"keywords": [
"throw",
"error",
"errors",
"exception",
"printf",
"format",
"wrap",
"inner",
"original",
"stack",
"properties"
],
"author": {
"name": "James Messinger",
"url": "https://jamesmessinger.com"
},
"license": "MIT",
"homepage": "https://jstools.dev/ono",
"repository": {
"type": "git",
"url": "https://github.com/JS-DevTools/ono.git"
},
"main": "cjs/index.js",
"module": "esm/index.js",
"typings": "esm/index.d.ts",
"browser": {
"./cjs/isomorphic.node.js": "./cjs/isomorphic.browser.js",
"./esm/isomorphic.node.js": "./esm/isomorphic.browser.js"
},
"files": [
"cjs",
"esm"
],
"scripts": {
"clean": "shx rm -rf .nyc_output coverage cjs esm",
"lint": "eslint src test",
"build": "npm run build:cjs && npm run build:esm",
"build:esm": "tsc",
"build:cjs": "tsc --module commonjs --outDir cjs",
"test": "npm run test:node && npm run test:typescript && npm run test:browser && npm run lint",
"test:node": "mocha",
"test:browser": "karma start --single-run",
"test:typescript": "tsc --noEmit test/specs/typescript.spec.ts",
"coverage": "npm run coverage:node && npm run coverage:browser",
"coverage:node": "nyc node_modules/mocha/bin/mocha",
"coverage:browser": "npm run test:browser -- --coverage",
"upgrade": "npm-check -u && npm audit fix",
"bump": "bump --tag --push --all",
"release": "npm run upgrade && npm run clean && npm run build && npm test && npm run bump"
},
"devDependencies": {
"@babel/polyfill": "^7.10.4",
"@jsdevtools/eslint-config": "^1.0.0",
"@jsdevtools/host-environment": "^2.0.3",
"@jsdevtools/karma-config": "^3.1.6",
"@jsdevtools/version-bump-prompt": "^6.0.3",
"@types/node": "^14.0.19",
"chai": "^4.2.0",
"eslint": "^7.4.0",
"karma": "^5.1.0",
"karma-cli": "^2.0.0",
"mocha": "^8.0.1",
"npm-check": "^5.9.2",
"nyc": "^15.1.0",
"shx": "^0.3.2",
"typescript": "^3.9.6"
},
"dependencies": {}
}