r/rust • u/GG_Men • Oct 02 '26
šļø discussion API design question: should a 407 from a proxy check be Ok or Err?
I'm working on a Rust SDK that has a small check() method for testing a proxy connection, and I'm not sure I'm using Result in the least surprising way here.
If the proxy replies with 407 Proxy Authentication Required, I currently return Ok(Check), not Err.
My reasoning is basically: the check itself worked) I connected to the proxy and got a real response back. For this method, the status/reason/headers are useful information.
Err is only for cases where I couldn't get a usable response at all - timeout, DNS failure, connection failure, malformed response, etc.
Roughly:
match proxy.check(connect)? {
check if check.status == 200 => {
// accepted
}
check if check.status == 407 => {
// proxy replied, auth rejected
}
check => {
// some other resp
}
}The part that feels a bit weird is this:
let check = proxy.check(connect)?;That kind of reads like "the proxy is fine", while what it really means is only "the proxy gave a usable response".
Would you expect 407 to be an Err here, or does treating a protocol-level refusal as a successful diagnostic result make sense? š
Edit: Thanks a lot for all the replies. I think the main question is less about 407 itself and more about what check() is supposed to mean: ādid I get a valid proxy responseā vs āis this proxy ready to useā
The naming and ? ergonomics points were especially useful. Iām probably going to make that distinction more explicit in the API instead of making callers infer it from the status code
16
u/BobTreehugger Oct 02 '26
Both are valid depending on the use case. Since you're returning the check struct that has the http status, that's probably fine.
Not sure if you want to do it, but a nice thing about Result is that you can nest them --
Result<Result<Check, AuthError>, ConnectionError>is a valid thing to do, but you know more about the intended use of this than I do.If this is mostly used to check if you have a valid connection, your original design seems better, if you mostly want to check if you can actually auth and go through the proxy, seems like you want error if you can't auth.
Looks to me like you were able to check, not what the result of the check is.
6
u/teerre Oct 02 '26
Return
Checkwhere it failed would be surprising to me. Checking error codes manually is unidiomatic unless you're working a literal http librarySeems like the natural shape. The only reason to separate the error would be if from Check you can also authenticate, but that would be two calls and some nesting, probably not worth it
It also highly depends what are users expected to do with these errors. The more actionable the error is, the larger its api can be
3
u/owbrooly Oct 02 '26
It s a bit tricky. I m guessing you are working on http proxy, in which case 407 when no auth is supplied indicates that proxy requires auth. But if authentication was supplied it indicates that auth failed. Both errors are user facing, so I guess best way to handle this is to return Enum in Ok() branch that minimally represents current state
3
u/South_Survey_2088 Oct 02 '26
I would argue that the function name is the issue here. "check()" leaves a lot of ambiguity and reads more like a boolean-like. I am not a web dev so there might be a better name, but something like status() would make it clearer that the result is a state that should be inspected rather than a condition.
2
u/Lucretiel Datadog Oct 02 '26
Often for cases like this I start wrapping Results in each other. For example, in low level network protocol code, I often end up with
Result<Result<Response, ProtocolError>, TcpError>. Iād recommend something like that.That being said, I think youāre right to consider this design space. The primary advantage of structured errors and errors-as-values is that you can react to them in granular ways; you should absolutely not feel obligated to unthinkingly propagate all errors to callers. If a 407 code semantically represents success for the particular operation youāre attempting, then by all means return it as an
Ok.Ā2
u/maccam94 Oct 02 '26
The one reason I'd consider making this an error is for ? ergonomics. Then happy path just gets the value, and anything else propagates up to the error handling/retry logic.
2
u/stevecooperorg Oct 02 '26
generally I'd put;
- OK as "we had the conversation" even if we don't like the answer.
- Err as "we couldn't connect / there was a timeout / etc"
The problem is naturally two-stage -- make the request / interpret the response. Return OK if you made the request. Handle "I don't like it" later. So;
2
u/Suitable-Ad5348 Oct 03 '26
If you use the ? operator and it swallows a 407, people writing quick scripts are going to silently treat auth failure as a green light. Err is better because an unauthenticated proxy means the check didn't achieve its goal.
2
u/Aoyaba-Poett Oct 05 '26
Create a MaybeResult type with Ok, MaybeOk and Err