Skip to content

client: distinguish malformed request from network/server errors in Exchange#142

Open
jamiesun wants to merge 1 commit into
layeh:masterfrom
talkincode:feature/exchange-error-typing
Open

client: distinguish malformed request from network/server errors in Exchange#142
jamiesun wants to merge 1 commit into
layeh:masterfrom
talkincode:feature/exchange-error-typing

Conversation

@jamiesun

@jamiesun jamiesun commented Jun 8, 2026

Copy link
Copy Markdown

Summary

client.Exchange() previously returned a bare error for every failure, so callers could not tell a malformed/un-encodable request apart from a network or server problem. That distinction matters for failover logic: a malformed request should not be retried against the next server, whereas a connection error should.

This adds a typed MalformedRequestError (with Unwrap) and wraps only the request-encoding failure in Exchange with it. Network errors continue to satisfy net.Error, and context errors remain context.DeadlineExceeded / context.Canceled, so callers can now branch cleanly:

resp, err := client.Exchange(ctx, req, addr)
var malformed *radius.MalformedRequestError
switch {
case errors.As(err, &malformed):
    // bad request, do not failover
case err != nil:
    // network/server error, try the next server
}

Changes

  • errors.go: add MalformedRequestError (Error, Unwrap) with usage doc.
  • client.go: wrap the encode failure path; document Exchange error types.
  • client_test.go: add TestClient_Exchange_malformedRequest.

No behavior change for existing callers that only check err != nil.

Closes #104

Client.Exchange previously returned the bare encoding error and the
underlying network error without any way to tell them apart. Callers
iterating over a list of RADIUS servers could not decide whether a
failure was caused by a malformed request (where trying the next server
is pointless) or by a connection/server problem (where failover helps).

Wrap encoding failures in a new *MalformedRequestError that unwraps to
the original error. Network failures continue to be returned as-is
(typically implementing net.Error), so callers can use errors.As to
distinguish the two cases.

Closes layeh#104

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Distinguish connection or server error during client.Exchange()

1 participant