Parse a `409` response body; `None` when it is not a usable JSON object. The flat `{"code", "message"}` spelling is expected, and the nested `{"error": {…}}` spelling is accepted so either server shape surfaces its reason.
(body: &str)
| 55 | /// nested `{"error": {…}}` spelling is accepted so either server shape |
| 56 | /// surfaces its reason. |
| 57 | fn from_body(body: &str) -> Option<Self> { |
| 58 | let parsed: Value = serde_json::from_str(body).ok()?; |
| 59 | let (code, message) = match parsed.get("error") { |
| 60 | Some(nested) => ( |
| 61 | nested.get("code").and_then(|v| v.as_str()), |
| 62 | nested.get("message").and_then(|v| v.as_str()), |
| 63 | ), |
| 64 | None => ( |
| 65 | parsed.get("code").and_then(|v| v.as_str()), |
| 66 | parsed.get("message").and_then(|v| v.as_str()), |
| 67 | ), |
| 68 | }; |
| 69 | Some(Self { |
| 70 | code: code.map(str::to_owned), |
| 71 | message: message.map(str::to_owned), |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// What the follow-up read of the remote binding resolved to after a `409`, |