http-rs / http-rs/surf

status code handling question

Open
#173 3 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.5k
Forks
128
PR merge metrics
No merged PRs in 30d

Description

I would ask this in chat but see #172 :/

recv_json seems to not check req.status for either is_success or for ==200. I'll happily grant that there are cases where API servers send some sort of sum type back
Enum {
Success(IP),
Failure(ErrorDescription)
}

(and that Enum, Success and Failure are all invisible on the wire and just made up to talk about this).

Anyhow, point is I grant that we might want to get JSON back in both success and error cases, but it is probably not the same JSON; Even if it is via the use of a Sum type, thats not always possible, because some API servers throw plain text errors.

And then you get the truely bad cases, where the error documents look the same as the success documents, and the only hint is that the results are not as long as one expected *plus* the HTTP status code is actually set to something useful.

So I guess my question is this: is there some way, for my paranoia to let me actually have surf check that the result was 200 before it parses the JSON, or do I have to forgo the convenience methods and write my own equivalents that check for HTTP status?

Also - it would be super super super helpful if the docs made clear which methods in surf take shortcuts like this and which don't. I'm not criticising the choice - I can see the pragmatism of 'if it deserialises, it is good data, and that way users don't need to deal with a whole class of errors'... but on the other hand, it can already throw, so :shrug:

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.