Optional: make ConnectError's non-exhaustiveness explicit (original semver premise was wrong)

Open
#245 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
48/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rust
Domain
api

Research direction

Start in connectrpc/src/error.rs and inspect ConnectError's existing private and pub(crate) fields, along with any in-crate struct literals. Decide whether adding #[non_exhaustive] provides useful explicit API hygiene; done is either adding the attribute without breaking in-crate construction or confirming that no change is needed.

Written by the indexing model from the issue text.

Description

Correction (2026-07-24): the premise below is wrong. ConnectError already has three non-public fields — http_status_override (private) plus response_headers and trailers (pub(crate)) — which appear after the three public ones in the struct definition. A struct with any non-pub field already cannot be built with a struct literal, nor exhaustively destructured, from outside the defining crate. So adding a field to ConnectError is not a breaking change today, and #[non_exhaustive] would be a no-op for downstream compilation.

I filed this after reading a truncated view of the struct that stopped just above the private fields. Apologies for the noise.

What remains, at much lower value: the attribute would make the existing guarantee explicit and machine-checked, rather than an emergent consequence of three fields happening to be non-public. If someone later made those fields public — for instance while addressing the reads that response_headers() / trailers() accessors currently serve — the semver protection would vanish silently. Limits carries the attribute for the same reason.

Keeping this open as optional hygiene, not as release-blocking work, and explicitly not as a reason to rush anything into the 0.9.0 window. Closing it as "not needed" is a perfectly good outcome.


Original report (superseded by the correction above)

ConnectError has public fields and no #[non_exhaustive], so adding any field to it is a breaking change for downstream crates.

// connectrpc/src/error.rs
/// A ConnectRPC error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectError {
    /// The error code.
    pub code: ErrorCode,
    /// Human-readable error message.
    pub message: Option<String>,
    /// Additional error details.
    pub details: Vec<ErrorDetail>,
}

Any consumer that writes ConnectError { code, message, details } as a struct literal, or destructures it exhaustively in a match, fails to compile the moment a field is added.

Why now

This is the type most likely to grow. #240 adds a source field to it, and #237 proposes carrying the transport error's source chain — both are field additions to this struct. Without the attribute each one is a semver break, which is a poor trade for what is otherwise additive behaviour.

Limits already carries #[non_exhaustive] for exactly this reason, so the crate is internally inconsistent as well.

Proposal

Add #[non_exhaustive] to ConnectError in 0.9.0, while the breaking-change window for the release is open.

The cost to downstream users is that struct-literal construction and exhaustive destructuring stop working; both are already discouraged for this type, which has constructors for every code (ConnectError::internal, ::unavailable, and so on) plus ConnectError::new. Field reads are unaffected, which is how the type is used in practice.

Worth deciding alongside it: whether the fields should stay pub at all. ClientConfig uses private fields with with_/accessor pairs and is the better-shaped example, but making these private is a larger break and can wait — #[non_exhaustive] alone captures the future-proofing that matters.

Notes

Cheap to do (one attribute plus any in-crate struct literals), and it stops being cheap the moment 0.9.0 ships.

Dominant language
Rust
Stars
544
Forks
77
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from connectrpc/connect-rust

All issues in connectrpc/connect-rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.