Skip to content

refactor: simplify Prismic client and error handling internals#225

Open
angeloashmore wants to merge 10 commits into
mainfrom
aa/refactor
Open

refactor: simplify Prismic client and error handling internals#225
angeloashmore wants to merge 10 commits into
mainfrom
aa/refactor

Conversation

@angeloashmore

@angeloashmore angeloashmore commented Jul 14, 2026

Copy link
Copy Markdown
Member

Resolves:

Description

Internal cleanup of the CLI. No user-facing behavior change is intended.

Reorganizes the Prismic client internals, collapses the auth token/host accessors into one, and moves per-command error wrapping to a single request-level option so error messages live next to the endpoints that produce them.

Checklist

  • A comprehensive Linear ticket, providing sufficient context and details to facilitate the review of the PR, is linked to the PR.
  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

How to QA


Note

Medium Risk
Large import-path and error-handling refactor across most commands and API clients; mistakes could change CLI error text or break endpoints, though behavior is intended to stay the same.

Overview
Internal refactor of the Prismic CLI with no intended user-facing behavior change. HTTP clients move from src/clients/* to src/lib/prismic/clients/*, with shared helpers per service and request options like notFoundMessage / unknownErrorMessage so commands no longer wrap every API call in UnknownRequestError handlers.

Auth replaces separate getToken() / getHost() with getCredentials() returning both values; adapters and commands are updated accordingly.

Field and model logic moves out of src/models into src/fields.ts (CLI targets/sources, getNewFieldTarget, reorder helpers) and src/lib/prismic/models (addField, reorderField, typed errors). The CLI entrypoint maps those model errors to user messages. Field add commands drop post-add hint output; field-reorder uses exactlyOneOption for --before / --after.

Smaller cleanups: environments under lib/prismic, push inlines dashboard URL helpers removed from wroom, type-remove / slice-remove delete by id without re-fetching the model, and the old amplitude client file is removed from this tree.

Reviewed by Cursor Bugbot for commit 5ae52a8. Bugbot is set up for automated code reviews on this repo. Configure here.

@angeloashmore angeloashmore marked this pull request as ready for review July 14, 2026 23:40

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ff6c53c. Configure here.

Comment thread src/lib/prismic/models.ts
targetFields[fieldId] = field;
await saveModel();
addField(fields, fieldId, field);
await save();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Field add hints removed

Low Severity

Every prismic field add command now logs only Field added: … and no longer prints the follow-up guidance that getPostFieldAddMessage used to emit after a successful add, despite the PR stating no intended user-facing behavior change.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ff6c53c. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's fine. It wasn't really useful.

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.

Vaguely related, but have you considered adding evals for the CLI (in future work)? Since it's partly (majoritarily?) intended to be used by AI, it might be nice to catch potential regressions related to messaging/API usage programmatically.

I like @vercel/agent-eval DX in that regard (I think I already shared: https://github.com/vercel-labs/agent-eval), maybe you'll prefer something smaller/not remote sandboxed.

Comment thread src/lib/prismic/clients/custom-types.ts
angeloashmore and others added 10 commits July 15, 2026 01:30
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Read response bodies once, add explicit json/body request options, and
move not-found messaging into request(). Standardize every client on
bare-base get*ServiceUrl builders and *ServiceRequest transport helpers,
with repository scoping handled in the helpers. Remove the unused
amplitude client and pass-through request helpers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add an endpoint-level `unknownErrorMessage` request option so client
functions declare their failure message once, instead of each command
wrapping calls in a try/catch that converts `UnknownRequestError` into a
`CommandError`. Removes 35 duplicated try/catch blocks across 26 command
files.

The message now lives on the endpoint, so it applies to every caller
(including previously bare calls to shared endpoints like getProfile and
the preview endpoints). The server response body is no longer appended.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
getToken() and getHost() were never called apart — 34 command, adapter,
and index.ts sites called them as a pair — and each independently read
credentials.json, so every command read the file from disk twice.

Replace both with one getCredentials() that reads the file once and
applies the same env-var precedence and default-host fallback. Call sites
collapse from two awaited lines to one.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- Drop the unused issues field and custom constructors from the config
  Invalid* error classes; nothing ever read them, so the read functions'
  catch blocks lose their ZodError branching.
- updateRoute delegates straight to addRoute/removeRoute (the page branch
  duplicated the read and hasRoute check addRoute already does).
- checkIsTypeScriptProject returns exists() directly.
- Unexport symbols only referenced inside the module (findConfigPath,
  findSuggestedConfigPath, findLegacySliceMachineConfigPath,
  MissingLegacySliceMachineConfigError, the Route type).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
removeLocale inherited the client-level "Repository not found" default,
so removing a non-existent locale reported the repository as missing.
Override with "Locale not found: <code>", matching the resource-specific
messages getCustomType/getSlice already use.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
deleteSlice/deleteCustomType already look up the resource by id and
throw the same not-found error, so the explicit get before delete did
no extra work.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Field add, edit, and reorder now surface duplicate and missing-field
errors using the full dotted path the user passed instead of only the
leaf segment.

Update and remove requests for a specific type or slice now report a
model-not-found message on 404 instead of the generic repository-not-
found message.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@lihbr lihbr left a comment

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.

LGTM, super rework!

Commands are much cleaner to read without try..catch or error throwing. I also like the new API used to manipulate models.

targetFields[fieldId] = field;
await saveModel();
addField(fields, fieldId, field);
await save();

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.

Vaguely related, but have you considered adding evals for the CLI (in future work)? Since it's partly (majoritarily?) intended to be used by AI, it might be nice to catch potential regressions related to messaging/API usage programmatically.

I like @vercel/agent-eval DX in that regard (I think I already shared: https://github.com/vercel-labs/agent-eval), maybe you'll prefer something smaller/not remote sandboxed.

Comment thread src/index.ts
Comment on lines 234 to 236
if (!UNTRACKED_COMMANDS.includes(command)) {
trackCommandEnd(command);
}

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.

💡 #idea: This bit is repeated on every case (some also track the error itself), maybe there's a way to abstract it before/after all the cases?

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