Skip to content

Commit

Permalink
fixup! name reform
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Feb 14, 2024
1 parent 6f16828 commit 97fe26b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 73 deletions.
36 changes: 8 additions & 28 deletions packages/pass-style/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';

Expand All @@ -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 {
Expand Down
50 changes: 8 additions & 42 deletions packages/pass-style/src/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="ses"/>

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 */
Expand Down Expand Up @@ -85,7 +85,7 @@ harden(isErrorLike);
* @param {Checker} [check]
* @returns {boolean}
*/
export const checkValidPassableErrorPropertyDesc = (
export const checkRecursivelyPassableErrorPropertyDesc = (
propName,
desc,
passStyleOfRecur,
Expand Down Expand Up @@ -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') {
Expand All @@ -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: {
Expand All @@ -147,15 +147,15 @@ export const checkValidPassableErrorPropertyDesc = (
reject(X`Passable Error has extra unpassed property ${q(propName)}`)
);
};
harden(checkValidPassableErrorPropertyDesc);
harden(checkRecursivelyPassableErrorPropertyDesc);

/**
* @param {unknown} candidate
* @param {import('./internal-types.js').PassStyleOf} passStyleOfRecur
* @param {Checker} [check]
* @returns {boolean}
*/
const checkValidPassableError = (
const checkRecursivelyPassableError = (
candidate,
passStyleOfRecur,
check = undefined,
Expand Down Expand Up @@ -186,7 +186,7 @@ const checkValidPassableError = (
}

return entries(descs).every(([propName, desc]) =>
checkValidPassableErrorPropertyDesc(
checkRecursivelyPassableErrorPropertyDesc(
propName,
desc,
passStyleOfRecur,
Expand All @@ -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);
71 changes: 68 additions & 3 deletions packages/pass-style/src/passStyleOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
/// <reference types="ses"/>

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';
Expand Down Expand Up @@ -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);

0 comments on commit 97fe26b

Please sign in to comment.