-
Notifications
You must be signed in to change notification settings - Fork 86
refactor(compartment-mapper): use @endo/errors #3152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /* Validates a compartment map against its schema. */ | ||
|
|
||
| import { Fail, q, b } from '@endo/errors'; | ||
| import { | ||
| assertPackagePolicy, | ||
| ATTENUATORS_COMPARTMENT, | ||
|
|
@@ -31,16 +32,11 @@ import { | |
| * DigestedCompartmentDescriptor} from './types.js' | ||
| */ | ||
|
|
||
| // TODO convert to the new `||` assert style. | ||
| // Deferred because this file pervasively uses simple template strings rather than | ||
| // template strings tagged with `assert.details` (aka `X`), and uses | ||
| // this definition of `q` rather than `assert.quote` | ||
| const q = JSON.stringify; | ||
| const { keys, entries } = Object; | ||
| const { isArray } = Array; | ||
|
|
||
| /** @type {(a: string, b: string) => number} */ | ||
| // eslint-disable-next-line no-nested-ternary | ||
| // eslint-disable-next-line no-nested-ternary, no-shadow | ||
| export const stringCompare = (a, b) => (a === b ? 0 : a < b ? -1 : 1); | ||
|
|
||
| /** | ||
|
|
@@ -83,12 +79,8 @@ function* enumerate(iterable) { | |
| * @returns {asserts value is string} | ||
| */ | ||
| const assertString = (value, pathOrMessage, url) => { | ||
| const keypath = pathOrMessage; | ||
| assert.typeof( | ||
| value, | ||
| 'string', | ||
| `${keypath} in ${q(url)} must be a string; got ${q(value)}`, | ||
| ); | ||
| typeof value === 'string' || | ||
| Fail`${b(pathOrMessage)} in ${q(url)} must be a string; got ${q(value)}`; | ||
|
Comment on lines
+82
to
+83
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I noted in Slack, I am not fond of
We could mitigate 1. by introducing a I have a branch where I've replaced all thrown errors (including usage of |
||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -97,7 +89,7 @@ const assertString = (value, pathOrMessage, url) => { | |
| * @param {unknown} allegedLabel | ||
| * @param {string} keypath | ||
| * @param {string} url | ||
| * @returns {asserts alleged is string} | ||
| * @returns {asserts allegedLabel is string} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| */ | ||
| const assertLabel = (allegedLabel, keypath, url) => { | ||
| assertString(allegedLabel, keypath, url); | ||
|
|
@@ -107,12 +99,10 @@ const assertLabel = (allegedLabel, keypath, url) => { | |
| if (allegedLabel === ENTRY_COMPARTMENT) { | ||
| return; | ||
| } | ||
| assert( | ||
| /^(?:@[a-z][a-z0-9-.]*\/)?[a-z][a-z0-9-.]*(?:>(?:@[a-z][a-z0-9-.]*\/)?[a-z][a-z0-9-.]*)*$/.test( | ||
| allegedLabel, | ||
| ), | ||
| `${keypath} must be a canonical name in ${q(url)}; got ${q(allegedLabel)}`, | ||
| ); | ||
| /^(?:@[a-z][a-z0-9-.]*\/)?[a-z][a-z0-9-.]*(?:>(?:@[a-z][a-z0-9-.]*\/)?[a-z][a-z0-9-.]*)*$/.test( | ||
| allegedLabel, | ||
| ) || | ||
| Fail`${b(keypath)} must be a canonical name in ${q(url)}; got ${q(allegedLabel)}`; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a little unclear on where we want to use |
||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -123,12 +113,10 @@ const assertLabel = (allegedLabel, keypath, url) => { | |
| */ | ||
| const assertPlainObject = (allegedObject, keypath, url) => { | ||
| const object = Object(allegedObject); | ||
| assert( | ||
| object === allegedObject && | ||
| !isArray(object) && | ||
| !(typeof object === 'function'), | ||
| `${keypath} must be an object; got ${q(allegedObject)} of type ${q(typeof allegedObject)} in ${q(url)}`, | ||
| ); | ||
| (object === allegedObject && | ||
| !isArray(object) && | ||
| !(typeof object === 'function')) || | ||
| Fail`${b(keypath)} must be an object; got ${q(allegedObject)} of type ${q(typeof allegedObject)} in ${q(url)}`; | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -139,19 +127,16 @@ const assertPlainObject = (allegedObject, keypath, url) => { | |
| * @returns {asserts value is boolean} | ||
| */ | ||
| const assertBoolean = (value, keypath, url) => { | ||
| assert.typeof( | ||
| value, | ||
| 'boolean', | ||
| `${keypath} in ${q(url)} must be a boolean; got ${q(value)}`, | ||
| ); | ||
| typeof value === 'boolean' || | ||
| Fail`${b(keypath)} in ${q(url)} must be a boolean; got ${q(value)}`; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {Record<string, unknown>} object | ||
| * @param {string} message | ||
| */ | ||
| const assertEmptyObject = (object, message) => { | ||
| assert(keys(object).length === 0, message); | ||
| keys(object).length === 0 || Fail`${b(message)}`; | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -161,10 +146,9 @@ const assertEmptyObject = (object, message) => { | |
| */ | ||
| const assertConditions = (conditions, url) => { | ||
| if (conditions === undefined) return; | ||
| assert( | ||
| isArray(conditions), | ||
| `conditions must be an array; got ${conditions} in ${q(url)}`, | ||
| ); | ||
| if (!isArray(conditions)) { | ||
| throw Fail`conditions must be an array; got ${b(String(conditions))} in ${q(url)}`; | ||
| } | ||
| for (const [index, value] of enumerate(conditions)) { | ||
| assertString(value, `conditions[${index}]`, url); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we're pulling in
@endo/errorsif all these symbols are already available inglobalThis. I'm missing some context.