@@ -18,13 +18,10 @@ pub struct TiledClient {
1818impl TiledClient {
1919 async fn request < T : DeserializeOwned > ( & self , endpoint : & str ) -> ClientResult < T > {
2020 println ! ( "Requesting data from tiled" ) ;
21-
2221 let url = self . address . join ( endpoint) ?;
23-
24- let response = reqwest:: get ( url) . await ?;
25- let json = response. json ( ) . await ?;
26-
27- Ok ( serde_json:: from_value ( json) ?)
22+ let response = reqwest:: get ( url) . await ?. error_for_status ( ) ?;
23+ let body = response. text ( ) . await ?;
24+ serde_json:: from_str ( & body) . map_err ( |e| ClientError :: InvalidResponse ( e, body) )
2825 }
2926}
3027impl Client for TiledClient {
@@ -35,38 +32,29 @@ impl Client for TiledClient {
3532
3633#[ derive( Debug ) ]
3734pub enum ClientError {
38- Parse ( url:: ParseError ) ,
39- Reqwest ( reqwest:: Error ) ,
40- Serde ( serde_json:: Error ) ,
41- Io ( std:: io:: Error ) ,
35+ InvalidPath ( url:: ParseError ) ,
36+ ServerError ( reqwest:: Error ) ,
37+ InvalidResponse ( serde_json:: Error , String ) ,
4238}
4339impl From < url:: ParseError > for ClientError {
4440 fn from ( err : url:: ParseError ) -> ClientError {
45- ClientError :: Parse ( err)
41+ ClientError :: InvalidPath ( err)
4642 }
4743}
4844impl From < reqwest:: Error > for ClientError {
4945 fn from ( err : reqwest:: Error ) -> ClientError {
50- ClientError :: Reqwest ( err)
51- }
52- }
53- impl From < serde_json:: Error > for ClientError {
54- fn from ( err : serde_json:: Error ) -> ClientError {
55- ClientError :: Serde ( err)
56- }
57- }
58- impl From < std:: io:: Error > for ClientError {
59- fn from ( err : std:: io:: Error ) -> ClientError {
60- ClientError :: Io ( err)
46+ ClientError :: ServerError ( err)
6147 }
6248}
49+
6350impl std:: fmt:: Display for ClientError {
6451 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
65- match * self {
66- ClientError :: Parse ( ref err) => write ! ( f, "Parse error: {}" , err) ,
67- ClientError :: Reqwest ( ref err) => write ! ( f, "Request error: {}" , err) ,
68- ClientError :: Serde ( ref err) => write ! ( f, "Serde error: {}" , err) ,
69- ClientError :: Io ( ref err) => write ! ( f, "IO Error: {}" , err) ,
52+ match self {
53+ ClientError :: InvalidPath ( err) => write ! ( f, "Invalid URL path: {}" , err) ,
54+ ClientError :: ServerError ( err) => write ! ( f, "Tiled server error: {}" , err) ,
55+ ClientError :: InvalidResponse ( err, actual) => {
56+ write ! ( f, "Invalid response: {err}, response: {actual}" )
57+ }
7058 }
7159 }
7260}
0 commit comments