Skip to content

refactor(compartment-mapper): use @endo/errors#3152

Open
erights wants to merge 1 commit intomasterfrom
markm-compartment-mapper-errors
Open

refactor(compartment-mapper): use @endo/errors#3152
erights wants to merge 1 commit intomasterfrom
markm-compartment-mapper-errors

Conversation

@erights
Copy link
Copy Markdown
Contributor

@erights erights commented Mar 31, 2026

Closes: #XXXX
Refs: 3fcb5b1#diff-2ce5fb201c10cb24e075359132ab825315f08bdd168d4c8e9ff464b87d3e74ffR4-R7 , #3110

Description

@boneskull reminded me of 3fcb5b1#diff-2ce5fb201c10cb24e075359132ab825315f08bdd168d4c8e9ff464b87d3e74ffR4-R7

This PR moves from the assert style towards the

    expr || Fail`...`

style, which is more efficient in the typical non-erroneous case, and is more idiomatic for Endo. It does this at the price of introducing a new dependency from @endo/compartment-mapper to @endo/error . If it is important for @endo/compartment-mapper not to transitively depend on ses, then this PR should probably wait on #3110 or a successor that accomplishes the same goal.

Security Considerations

None

Scaling Considerations

If this PR waits for #3110 or equivalent, then none. Otherwise, the transitive dependency on ses may be significant. OTOH, @endo/compartment-mapper's use of ses's assert shows that it is already transitively dependent, so probably makes no difference either way.

Documentation Considerations

We want to encourage the ... || Fail... style over the assert style, so good to get rid of violations of our own style preferences in our own code.

Testing Considerations

All existing tests pass without modification, which should be good enough for this refactoring PR.

Compatibility Considerations

Some error messages that were formed with untagged template literals are now formed with Fail. Untagged template literals don't redact, which seems to be intentional here. To preserve that intention, this PR annotations all Fail interpolations that did not already have a q(...) with a b(...).

Upgrade Considerations

none

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 31, 2026

⚠️ No Changeset found

Latest commit: a8f1243

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@erights erights self-assigned this Mar 31, 2026
@erights erights requested a review from boneskull March 31, 2026 01:16
@erights erights marked this pull request as ready for review March 31, 2026 01:31
@erights erights requested a review from kriskowal March 31, 2026 01:32
Copy link
Copy Markdown
Member

@boneskull boneskull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So while I don't have a major problem with landing this, I'll likely change it later once we land support for Error.code; I want all exceptions thrown (directly) out of @endo/compartment-mapper to have a proper error code.

Please let the record show I didn't ask for an implementation. 😉 Mostly I was curious about whether I needed to use details/X anywhere, and how. I should have been more specific. That said, I don't even know when to use b/bare, so I'm hoping you can enlighten me.

The reason behind the original ask was that I'm also looking into partitioning the assertions here into this module for CompartmentMapDescriptor-specific assertions and another for general, reusable assertions (e.g., assertPlainObject, assertFileUrlString), with an eye towards enabling PackageCompartmentMapDescriptor validation before returning from mapNodeModules. I saw the TODO and didn't want to propagate the same problem to a new module.

@@ -1,5 +1,6 @@
/* Validates a compartment map against its schema. */

import { Fail, q, b } from '@endo/errors';
Copy link
Copy Markdown
Member

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/errors if all these symbols are already available in globalThis. I'm missing some context.

Comment on lines +82 to +83
typeof value === 'string' ||
Fail`${b(pathOrMessage)} in ${q(url)} must be a string; got ${q(value)}`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I noted in Slack, I am not fond of Fail generally for two reasons:

  1. It cannot support Error.code
  2. When used outside of an asserts type guard, TS cannot infer that Fail terminates unless throw is used. This makes correct usage of Fail rather tricky.

We could mitigate 1. by introducing a Fail.code(str)-like API, but that's very much a separate PR & discussion.

I have a branch where I've replaced all thrown errors (including usage of Fail, throw new Error(), & throw Error()) with a wrapper using makeError(), built on top of #3130. While this would be fine to merge as-is, I would want to replace the Fail calls (regardless of whether Fail.code() becomes a thing).

* @param {string} keypath
* @param {string} url
* @returns {asserts alleged is string}
* @returns {asserts allegedLabel is string}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/^(?:@[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)}`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little unclear on where we want to use b/bare vs q/quote; obviously the latter is for strings, but from what I can tell, keypath is also a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants