Skip to content

feat: give core's own error sentences their own codes - #143

Merged
fylorn merged 1 commit into
mainfrom
feat/coded-errors
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/coded-errors

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Why

Fourteen codes were pure passthrough wrappers: the English template was "{detail}" and detail was the Display of some core error. The UI could only translate the code, which said nothing, so the Chinese UI showed the English sentence underneath. Worse, several paths flattened an already-coded Msg back into text (invalid(e.text), client_for(...).map_err(|e| e.message()), the price-sheet draft double wrap) before wrapping it again.

What changed

Core's own sentences now carry their own codes. Each error enum gets a msg() with one msg! per variant, next to its definition; its Display is #[error("{}", self.msg())], so the English is written exactly once.

Wrapper (removed) What detail was Now
control.adopt_failed, control.mcp_failed, control.mcp_target_unknown tw_adopt PlanError / ForeignError / McpError adopt.plan.*, adopt.file.*, adopt.mcp.*; an unverified MCP target speaks with its existing reason code
control.bad_credential CredentialError config.credential.*
control.bad_price_sheet SheetError (or an already-coded name error) pricing.sheet.*, or the name error as is
control.bad_rule, control.route_failed routes::to_rule strings, RouteError control.rule.* (every one names the rule), engine.*, engine.compare.*
control.config_rejected ApplyError: store, validation, edit, patch, in-use and the flattened resource errors each cause's own code: config.store.*, config.* (validation), config.edit.*, yaml.*, control.*_in_use, control.group.*, control.patch.*, control.config_stale, … ApplyError gains Invalid(Msg); InUse/BadPath/Build carry a Msg
control.listen.bad_bind Bind's own serde message control.listen.bind_invalid
control.pricing_refresh_failed RefreshError built from format! control.pricing.*
control.upstream_call_failed data-plane GatewayError text, a serde error, Z.ai call strings the data plane's own gw.* Msg; control.device_code_unreadable; control.account_service_*
control.internal SQLite errors, a poisoned lock, a YAML serializer, StoreError control.records_unreadable, control.internal_error, config.store.*
l1.io std::io::Error l1.io.refused, .reset, .aborted, .timed_out, .addr_not_available, .not_found, .permission_denied, .eof, .broken_pipe, .host_unreachable, .network_unreachable. l1.io stays as the fallback for other kinds. tcp_message now decides by code, not by the OS wording

Text from outside (SQLite, serde, regex, reqwest, the OS, an upstream's body) stays in a detail arg inside a coded sentence. tw_config::Rejected carries a Msg: semantic errors use their own code, and serde's words get a coded frame (config.rejected / config.rejected_at with stage and line). The ConfigRejected event keeps sending the text.

Two nested cases keep the inner code and add one arg, rather than doubling the code table: ValidationError::Credential adds upstream to the credential code, and a comparison error inside a route rule adds rule to the engine.compare.* code. In both cases the English text gets the matching prefix.

Small cleanups found along the way: PlanError::SecretGone was never constructed and is gone; BadCidr and the empty-pattern case no longer carry a fixed English reason/detail string.

HTTP status codes are unchanged.

Remaining {detail}-only codes

  • l1.io: io kinds other than the eleven above (raw OS text)
  • config.unparsable: serde's message inside Rejected. It only reaches the UI through the config.rejected* frame (and as text in the ConfigRejected event)
  • config.credential.env: ${VAR} expansion failures. These happen only at forward time, never in the credential checks that produce a Msg

Tests

Every converted enum has a test that each variant yields its own non-empty code, unique within the enum, whose text equals Display. Wrapped variants are checked to use the inner code. to_rule / to_group failures, io kind mapping, tcp_message and CallError are covered too.

Checked locally: cargo fmt --all --check, cargo clippy --all-targets -- -D warnings, cargo clippy -p <touched> --lib -- -D warnings and cargo test --workspace.

🤖 Generated with Claude Code

Fourteen control-plane codes (and l1.io) were pure passthrough wrappers:
the template was "{detail}" and the detail was the Display of some core
error, so a translated UI still showed English. Each of those errors now
carries its own coded Msg instead.

- tw-yaml PatchError, tw-engine RouteError/MatchError, tw-pricing
  SheetError, tw-config CredentialError/StoreError/EditError/
  ValidationError, tw-adopt PlanError/ForeignError/McpError and
  tw-control RefreshError get a msg() with one code per variant; their
  Display is that Msg's text, so the English is written once.
- tw_config::Rejected carries a Msg: semantic errors speak with their
  own code, serde's words get a coded frame (stage and line).
- ApplyError keeps each cause's Msg (Invalid/InUse/BadPath/Build carry
  one) instead of an English string, so route, group, key and resource
  validation reaches the UI with its own code.
- l1 maps the common io::ErrorKinds to codes; l1.io with {detail}
  remains only for the rest.
- Truly external text (SQLite, serde, reqwest, OS) keeps a detail arg
  inside a coded sentence.

Removed codes: control.adopt_failed, control.bad_credential,
control.bad_price_sheet, control.bad_rule, control.config_rejected,
control.internal, control.listen.bad_bind, control.mcp_failed,
control.mcp_target_unknown, control.pricing_refresh_failed,
control.route_failed, control.upstream_call_failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit b30e51e into main Sep 23, 2026
2 checks passed
@fylorn
fylorn deleted the feat/coded-errors branch September 23, 2026 15:28
@fylorn fylorn mentioned this pull request Sep 23, 2026
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.

1 participant