Skip to content

Typed ErrorKind: classify vendor codes once, convert all domain modules, CLI exit codes (#62) - #64

Merged
IRmsk merged 1 commit into
IRmsk:mainfrom
AptS-1547:refactor/error-kinds
Nov 26, 2026
Merged

IRmsk merged 1 commit into
IRmsk:mainfrom
AptS-1547:refactor/error-kinds

Conversation

@AptS-1547

@AptS-1547 AptS-1547 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #62.

Summary

Replaces Result<_, String> across the domain layers with a typed ErrorKind, completing all three steps of the issue's rollout:

Type (src/error.rs): Auth / Protocol{code, msg} / Network / RateLimit / Fence / Client / Storage(io) / Json(serde) / Other, Chinese explanations as Display. One extension of the issue sketch: Client carries local request-construction failures (clock skew, missing device keys, unsupported method) — folding those into Network would mislabel "系统时间异常" as a network problem. Other(String) is the documented migration tail for local data-shape messages not yet assigned a specific variant; the known semantics (vendor codes, geometry, IO, auth) all live in concrete variants.

Step 1 — classification at the choke point: client.rs (request + expect_ok) classifies once: 10121/10175 → Auth, 10603 → RateLimit, others → Protocol; transport failures → Network. classify_vendor is the only place a vendor business code becomes a category.

Step 2 — all domain modules converted: client, login, records, semester, cheat, ai, flow, submit, fence, points, policy, obs, gmttime, gt4, gt4image, session, accounts, guard, start_time, egress, audit, crosscheck, device_table + track (roadnet, tileroad, fence_route, planner, simulate). Result<_, String> is zero across src/api/ and src/track/; every transition shim from the first step is removed. The web layer keeps Result<(), String> for its own plumbing — those are tiny_http response-write failures, not domain errors (documented in the module docs).

Step 3 — consumers:

  • Web routes answer through json_err_kind: precise statuses (401 auth / 429 rate-limit / 502 upstream) plus structured kind and vendor_code fields.
  • CLI: ErrorKind::exit_code() maps each variant to a distinct code 10–18 (10 auth, 11 protocol, 12 network, 13 rate limit, 14 fence, 15 local construction, 16 storage, 17 json, 18 other), chosen not to collide with the existing 0/1/2/3; typed error paths in main.rs exit with their class, so scripts branch on exit codes instead of parsing Chinese prose.

Behavior

  • Vendor-facing: unchanged. Same requests, same payloads, same retry triggers — classification is internal.
  • Console HTTP: intentionally changes (the point of Proposal: replace Result<_, String> with typed error kinds #62) — vendor errors move from blanket 200/400 to 401/429/502 with kind/vendor_code. Documented in CHANGELOG.
  • CLI: printed wording shifts slightly (structured Chinese), exit codes gained the 10–18 class range; existing 0/1/2/3 untouched.

Verification

  • 532 lib tests (classification, status/kind/exit-code tables, From impls, legacy-message alignment) + integration tests green. cargo clippy --all-targets 0 warnings, fmt --check clean, compliant with the Proposal: workspace lint baseline (unsafe forbid, missing_docs, clippy::pedantic + cast denies) #58 lint baseline.
  • -10000 cluster-fault guidance asserted verbatim (survives as Protocol{code:-10000} with the full "别折腾本地环境" explanation); 10121 asserts Auth instead of a substring contract.
  • Live-verified against the real server: a smoke login with wrong credentials got the genuine vendor rejection 10175 → HTTP 401 + kind=auth + structured Chinese message; that code was added to classify_vendor with a test.
  • Non-typed CLI paths verified unchanged: audit on a missing file still exits 1, version exits 0.

163 of 168 Result signatures return Result<_, String>, so every vendor
rejection, expired session and network timeout funnels into the same
Chinese prose string - and the web console turns all of them into the
same response, with no kind field to branch on.

First step of IRmsk#62: introduce src/error.rs with ErrorKind (Auth /
Protocol{code,msg} / Network / RateLimit / Fence / Client / Storage /
Json), one classify_vendor() as the single place a vendor business code
becomes a category (10121/10175 -> Auth, 10603 -> RateLimit, others ->
Protocol), and http_status()/kind_name() mappings. Client extends the
issue sketch with a Client variant for local construction failures
(clock, keys, unsupported method) - those are not network problems.

Converted in this step: client.rs (request/expect_ok classify at the
boundary), login / records / semester / cheat / ai, and the web routes
backed by them now answer with precise statuses (401 auth / 429 rate
limit / 502 upstream) plus structured kind and vendor_code fields.
flow/submit/fence/points/policy/obs keep String signatures with an
explicit to_string() boundary and TODO(IRmsk#62) markers; they convert next.

Vendor-facing behavior is unchanged (same requests, same retry
triggers). Console HTTP responses intentionally change for these
endpoints - that is the point of IRmsk#62 - and CLI printed wording shifts
slightly; exit codes are untouched (step 3 of the rollout). Verified
live: a real vendor rejection (10175 wrong password, from a smoke run
against the actual server) surfaces as 502 + kind=protocol +
vendor_code=10175, and is now classified Auth in classify_vendor.
@IRmsk
IRmsk merged commit 0302b87 into IRmsk:main Nov 26, 2026
8 checks passed
@IRmsk

IRmsk commented Nov 26, 2026

Copy link
Copy Markdown
Owner

合了:0302b87。

我在本地把它和 main 上刚加的 unwrap/expect/panic 禁令(a855ad5)合起来跑了一遍:fmt、clippy、555 个测试、cargo-deny 都过,没有一处需要改你的代码。

顺手核了一个你没提的点:前端 core.js 的 api() 在非 200 时照样读 error 字段,所以 401/429/502 之后页面上显示的还是那句中文,不会变成「请求失败」。

Client 这个变体加得对,「系统时间异常」确实不该算网络问题。

@AptS-1547 AptS-1547 changed the title Typed ErrorKind: classify vendor codes once at the client boundary (step 1 of #62) Typed ErrorKind: classify vendor codes once, convert all domain modules, CLI exit codes (#62) Nov 26, 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.

Proposal: replace Result<_, String> with typed error kinds

2 participants