@@ -18,13 +18,12 @@ 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) ?;
22+
23+ let response = reqwest:: get ( url) . await ?. error_for_status ( ) ?;
24+ let body = response. text ( ) . await ?;
25+ serde_json:: from_str ( & body) . map_err ( |e| ClientError :: InvalidResponse ( e, body) )
2326
24- let response = reqwest:: get ( url) . await ?;
25- let json = response. json ( ) . await ?;
26-
27- Ok ( serde_json:: from_value ( json) ?)
2827 }
2928}
3029impl Client for TiledClient {
@@ -35,38 +34,29 @@ impl Client for TiledClient {
3534
3635#[ derive( Debug ) ]
3736pub enum ClientError {
38- Parse ( url:: ParseError ) ,
39- Reqwest ( reqwest:: Error ) ,
40- Serde ( serde_json:: Error ) ,
41- Io ( std:: io:: Error ) ,
37+ InvalidPath ( url:: ParseError ) ,
38+ ServerError ( reqwest:: Error ) ,
39+ InvalidResponse ( serde_json:: Error , String ) ,
4240}
4341impl From < url:: ParseError > for ClientError {
4442 fn from ( err : url:: ParseError ) -> ClientError {
45- ClientError :: Parse ( err)
43+ ClientError :: InvalidPath ( err)
4644 }
4745}
4846impl From < reqwest:: Error > for ClientError {
4947 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)
48+ ClientError :: ServerError ( err)
6149 }
6250}
51+
6352impl std:: fmt:: Display for ClientError {
6453 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) ,
54+ match self {
55+ ClientError :: InvalidPath ( err) => write ! ( f, "Invalid URL path: {}" , err) ,
56+ ClientError :: ServerError ( err) => write ! ( f, "Tiled server error: {}" , err) ,
57+ ClientError :: InvalidResponse ( err, actual) => {
58+ write ! ( f, "Invalid response: {err}, response: {actual}" )
59+ }
7060 }
7161 }
7262}
0 commit comments