Befor generating
This commit is contained in:
+3
@@ -0,0 +1,3 @@
|
||||
import { OnoConstructor } from "./types";
|
||||
declare const constructor: OnoConstructor;
|
||||
export { constructor as Ono };
|
||||
+44
@@ -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
|
||||
+1
@@ -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,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AACH,CAAC,CAAC"}
|
||||
+9
@@ -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
@@ -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
|
||||
+1
@@ -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,MAAM,WAAW,CAAC;AAGhD,MAAM,cAAc,GAA2B,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAE5E;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAA6D,KAAQ,EAAE,aAAiB,EAAE,KAAS;IAC5H,IAAI,QAAQ,GAAG,KAAmD,CAAC;IAEnE,WAAW,CAAC,QAAQ,EAAE,aAAa,CAAC,C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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
@@ -0,0 +1,5 @@
|
||||
import { ono } from "./singleton";
|
||||
export { Ono } from "./constructor";
|
||||
export * from "./types";
|
||||
export { ono };
|
||||
export default ono;
|
||||
+11
@@ -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
@@ -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"}
|
||||
+15
@@ -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;
|
||||
+16
@@ -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
|
||||
+1
@@ -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"}
|
||||
+15
@@ -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;
|
||||
+41
@@ -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
|
||||
+1
@@ -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
@@ -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
@@ -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
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAG3C;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAoB;IACnD,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;IACxB,OAAO;QACL,cAAc,EAAE,OAAO,CAAC,cAAc,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC;QAC7F,MAAM,EAAE,OA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O,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
@@ -0,0 +1,3 @@
|
||||
import { OnoSingleton } from "./types";
|
||||
declare const singleton: OnoSingleton;
|
||||
export { singleton as ono };
|
||||
+34
@@ -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
|
||||
+1
@@ -0,0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C,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
@@ -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
@@ -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
@@ -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;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,SAAyC;IACvE,OAAO,OAAO;IACZ,uFAAuF;IACvF,CAAC,SAAS;QACV,SAAS,CAAC,QAAQ;QAClB,OAAO,SAAS,CAAC,GAAG,KAAK,UAAU,CACpC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,QAAmB,EAAE,aAAyB;IACvE,IAAI,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACxC,IAAI,aAAa,GAAG,aAAa,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IAEpE,IAAI,QAAQ,IAAI,aAAa,EAAE;QAC7B,OAAO,QAAQ,GAAG,MAAM,GAAG,aAAa,CAAC;KAC1C;SACI;QACH,OAAO,QAAQ,IAAI,aAAa,CAAC;KAClC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,C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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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,2 @@
|
||||
import { inspect } from "util";
|
||||
//# sourceMappingURL=types.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC"}
|
||||
Reference in New Issue
Block a user