From 97fe26b462e2d9bdfa1a9f8626f175d6bb2b9b95 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Wed, 14 Feb 2024 13:53:00 -0800 Subject: [PATCH] fixup! name reform --- packages/pass-style/index.js | 36 +++---------- packages/pass-style/src/error.js | 50 +++--------------- packages/pass-style/src/passStyleOf.js | 71 ++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 73 deletions(-) diff --git a/packages/pass-style/index.js b/packages/pass-style/index.js index 6bf1199c17..2584cdba1b 100644 --- a/packages/pass-style/index.js +++ b/packages/pass-style/index.js @@ -1,7 +1,3 @@ -import { checkValidPassableErrorPropertyDesc } from './src/error.js'; -import { assertChecker } from './src/passStyle-helpers.js'; -import { passStyleOf } from './src/passStyleOf.js'; - export { mapIterable, filterIterable } from './src/iter-helpers.js'; export { PASS_STYLE, @@ -11,29 +7,7 @@ export { hasOwnPropertyOf, } from './src/passStyle-helpers.js'; -export { - getErrorConstructor, - toPassableError, - isErrorLike, -} from './src/error.js'; - -/** - * @param {string} name - * @param {PropertyDescriptor} desc - * @returns {boolean} - */ -export const isValidErrorPropertyDesc = (name, desc) => - checkValidPassableErrorPropertyDesc(name, desc, passStyleOf); -harden(isValidErrorPropertyDesc); - -/** - * @param {string} name - * @param {PropertyDescriptor} desc - * @returns {boolean} - */ -export const assertValidErrorPropertyDesc = (name, desc) => - checkValidPassableErrorPropertyDesc(name, desc, passStyleOf, assertChecker); -harden(assertValidErrorPropertyDesc); +export { getErrorConstructor, isErrorLike } from './src/error.js'; export { getInterfaceOf } from './src/remotable.js'; @@ -44,7 +18,13 @@ export { passableSymbolForName, } from './src/symbol.js'; -export { passStyleOf, assertPassable } from './src/passStyleOf.js'; +export { + passStyleOf, + assertPassable, + isPassableErrorPropertyDesc, + assertPassableErrorPropertyDesc, + toPassableError, +} from './src/passStyleOf.js'; export { makeTagged } from './src/makeTagged.js'; export { diff --git a/packages/pass-style/src/error.js b/packages/pass-style/src/error.js index aacd46fe27..4d3fc9258f 100644 --- a/packages/pass-style/src/error.js +++ b/packages/pass-style/src/error.js @@ -1,6 +1,6 @@ /// -import { X, annotateError, makeError, q } from '@endo/errors'; +import { X, q } from '@endo/errors'; import { assertChecker } from './passStyle-helpers.js'; /** @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper */ @@ -85,7 +85,7 @@ harden(isErrorLike); * @param {Checker} [check] * @returns {boolean} */ -export const checkValidPassableErrorPropertyDesc = ( +export const checkRecursivelyPassableErrorPropertyDesc = ( propName, desc, passStyleOfRecur, @@ -122,7 +122,7 @@ export const checkValidPassableErrorPropertyDesc = ( } case 'cause': { // eslint-disable-next-line no-use-before-define - return checkValidPassableError(value, passStyleOfRecur, check); + return checkRecursivelyPassableError(value, passStyleOfRecur, check); } case 'errors': { if (!Array.isArray(value) || passStyleOfRecur(value) !== 'copyArray') { @@ -135,7 +135,7 @@ export const checkValidPassableErrorPropertyDesc = ( } return value.every(err => // eslint-disable-next-line no-use-before-define - checkValidPassableError(err, passStyleOfRecur, check), + checkRecursivelyPassableError(err, passStyleOfRecur, check), ); } default: { @@ -147,7 +147,7 @@ export const checkValidPassableErrorPropertyDesc = ( reject(X`Passable Error has extra unpassed property ${q(propName)}`) ); }; -harden(checkValidPassableErrorPropertyDesc); +harden(checkRecursivelyPassableErrorPropertyDesc); /** * @param {unknown} candidate @@ -155,7 +155,7 @@ harden(checkValidPassableErrorPropertyDesc); * @param {Checker} [check] * @returns {boolean} */ -const checkValidPassableError = ( +const checkRecursivelyPassableError = ( candidate, passStyleOfRecur, check = undefined, @@ -186,7 +186,7 @@ const checkValidPassableError = ( } return entries(descs).every(([propName, desc]) => - checkValidPassableErrorPropertyDesc( + checkRecursivelyPassableErrorPropertyDesc( propName, desc, passStyleOfRecur, @@ -204,39 +204,5 @@ export const ErrorHelper = harden({ canBeValid: checkErrorLike, assertValid: (candidate, passStyleOfRecur) => - checkValidPassableError(candidate, passStyleOfRecur, assertChecker), + checkRecursivelyPassableError(candidate, passStyleOfRecur, assertChecker), }); - -/** - * Return a new passable error that propagates the diagnostic info of the - * original, and is linked to the original as a note. - * - * @param {Error | AggregateError} err - * @returns {Error} - */ -export const toPassableError = err => { - const { - name, - message, - cause = undefined, - // @ts-expect-error err might be an AggregateError, which would - // have an `errors` property. In addition, we tolerate `errors` on - // other errors, just like we do `cause`. - errors = undefined, - } = err; - - const errConstructor = getErrorConstructor(`${name}`) || Error; - const newError = harden( - makeError(`${message}`, errConstructor, { - // @ts-ignore Assuming cause is Error | undefined - cause, - errors, - }), - ); - // Even the cleaned up error copy, if sent to the console, should - // cause hidden diagnostic information of the original error - // to be logged. - annotateError(newError, X`copied from error ${err}`); - return newError; -}; -harden(toPassableError); diff --git a/packages/pass-style/src/passStyleOf.js b/packages/pass-style/src/passStyleOf.js index a335b2bca2..c51e8168e0 100644 --- a/packages/pass-style/src/passStyleOf.js +++ b/packages/pass-style/src/passStyleOf.js @@ -3,13 +3,22 @@ /// import { isPromise } from '@endo/promise-kit'; -import { X, Fail, q } from '@endo/errors'; -import { isObject, isTypedArray, PASS_STYLE } from './passStyle-helpers.js'; +import { X, Fail, q, annotateError, makeError } from '@endo/errors'; +import { + assertChecker, + isObject, + isTypedArray, + PASS_STYLE, +} from './passStyle-helpers.js'; import { CopyArrayHelper } from './copyArray.js'; import { CopyRecordHelper } from './copyRecord.js'; import { TaggedHelper } from './tagged.js'; -import { ErrorHelper } from './error.js'; +import { + ErrorHelper, + checkRecursivelyPassableErrorPropertyDesc, + getErrorConstructor, +} from './error.js'; import { RemotableHelper } from './remotable.js'; import { assertPassableSymbol } from './symbol.js'; @@ -221,3 +230,59 @@ export const assertPassable = val => { passStyleOf(val); // throws if val is not a passable }; harden(assertPassable); + +/** + * @param {string} name + * @param {PropertyDescriptor} desc + * @returns {boolean} + */ +export const isPassableErrorPropertyDesc = (name, desc) => + checkRecursivelyPassableErrorPropertyDesc(name, desc, passStyleOf); +harden(isPassableErrorPropertyDesc); + +/** + * @param {string} name + * @param {PropertyDescriptor} desc + * @returns {boolean} + */ +export const assertPassableErrorPropertyDesc = (name, desc) => + checkRecursivelyPassableErrorPropertyDesc( + name, + desc, + passStyleOf, + assertChecker, + ); +harden(assertPassableErrorPropertyDesc); + +/** + * Return a new passable error that propagates the diagnostic info of the + * original, and is linked to the original as a note. + * + * @param {Error | AggregateError} err + * @returns {Error} + */ +export const toPassableError = err => { + const { + name, + message, + cause = undefined, + // @ts-expect-error err might be an AggregateError, which would + // have an `errors` property. In addition, we tolerate `errors` on + // other errors, just like we do `cause`. + errors = undefined, + } = err; + + const errConstructor = getErrorConstructor(`${name}`) || Error; + const newError = makeError(`${message}`, errConstructor, { + // @ts-ignore Assuming cause is Error | undefined + cause, + errors, + }); + harden(newError); + // Even the cleaned up error copy, if sent to the console, should + // cause hidden diagnostic information of the original error + // to be logged. + annotateError(newError, X`copied from error ${err}`); + return newError; +}; +harden(toPassableError);