@@ -16,17 +16,19 @@ struct TopLevelError<T> {
1616 pub error : T ,
1717}
1818
19+ /// This is actually [`auth::RateLimitError`] but re-imported here with less type info because the
20+ /// auth namespace is not necessarily compiled in, but this must always be available.
1921#[ derive( Debug , Deserialize ) ]
2022struct RateLimitedError {
21- pub reason : RateLimitedReason ,
23+ pub reason : OpenUnionTag ,
2224
2325 #[ serde( default ) ] // too_many_write_operations errors don't include this field; default to 0.
2426 pub retry_after : u32 ,
2527}
2628
27- // Rather than deserializing into an enum, just capture the tag value.
29+ /// Rather than deserializing into an enum, just capture the tag value.
2830#[ derive( Debug , Deserialize ) ]
29- struct RateLimitedReason {
31+ struct OpenUnionTag {
3032 #[ serde( rename = ".tag" ) ]
3133 tag : String ,
3234}
@@ -75,8 +77,29 @@ pub fn request_with_body<T: DeserializeOwned, E: DeserializeOwned + StdError, P:
7577 Err ( Error :: BadRequest ( response) )
7678 } ,
7779 401 => {
78- Err ( Error :: InvalidToken ( response) )
80+ match serde_json:: from_str :: < TopLevelError < OpenUnionTag > > ( & response) {
81+ Ok ( deserialized) => {
82+ error ! ( "invalid token: {}" , deserialized. error. tag) ;
83+ Err ( Error :: InvalidToken ( deserialized. error . tag ) )
84+ }
85+ Err ( de_error) => {
86+ error ! ( "Failed to deserialize JSON from API error: {}" , de_error) ;
87+ Err ( Error :: Json ( de_error) )
88+ }
89+ }
7990 } ,
91+ 403 => {
92+ match serde_json:: from_str :: < TopLevelError < serde_json:: Value > > ( & response) {
93+ Ok ( deserialized) => {
94+ error ! ( "access denied: {:?}" , deserialized. error) ;
95+ Err ( Error :: AccessDenied ( deserialized. error ) )
96+ }
97+ Err ( de_error) => {
98+ error ! ( "Failed to deserialize JSON from API error: {}" , de_error) ;
99+ Err ( Error :: Json ( de_error) )
100+ }
101+ }
102+ }
80103 409 => {
81104 // Response should be JSON-deseraializable into the strongly-typed
82105 // error specified by type parameter E.
0 commit comments