11package api
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
7+ "net"
68 "net/http"
9+ "os"
10+ "strings"
711 "time"
812)
913
1014// ----------------------------------------------
1115// const endpoints
1216// ----------------------------------------------
17+
1318const DOMAIN = ".pamdas.org"
1419
1520const API_V1 = "/api/v1.0"
@@ -85,7 +90,7 @@ func (c *Client) newRequest(method, endpoint string, isAuth bool) (*http.Request
8590func (c * Client ) doRequest (req * http.Request , v interface {}) error {
8691 resp , err := c .httpClient .Do (req )
8792 if err != nil {
88- return fmt . Errorf ( "failed to make request: %w" , err )
93+ return c . handleRequestError ( err )
8994 }
9095 defer resp .Body .Close ()
9196
@@ -100,6 +105,36 @@ func (c *Client) doRequest(req *http.Request, v interface{}) error {
100105 return nil
101106}
102107
108+ func (c * Client ) handleRequestError (err error ) error {
109+ // Check for timeout errors
110+ if os .IsTimeout (err ) {
111+ return fmt .Errorf ("request timed out - try reducing the requested data and/or check your network connection" )
112+ }
113+
114+ // Check for context deadline exceeded (another form of timeout)
115+ if err == context .DeadlineExceeded || strings .Contains (err .Error (), "context deadline exceeded" ) {
116+ return fmt .Errorf ("request timed out - try reducing the date range with --days or check your network connection" )
117+ }
118+
119+ // Check for network timeout specifically
120+ if netErr , ok := err .(net.Error ); ok && netErr .Timeout () {
121+ return fmt .Errorf ("network timeout - try reducing the date range with --days or check your network connection" )
122+ }
123+
124+ // Check for DNS resolution errors
125+ if strings .Contains (err .Error (), "no such host" ) {
126+ return fmt .Errorf ("unable to connect to EarthRanger server - check your network connection and site name" )
127+ }
128+
129+ // Check for connection refused
130+ if strings .Contains (err .Error (), "connection refused" ) {
131+ return fmt .Errorf ("connection refused - check your network connection and EarthRanger server status" )
132+ }
133+
134+ // Default error message for other request failures
135+ return fmt .Errorf ("failed to connect to EarthRanger server: %v" , err )
136+ }
137+
103138// ----------------------------------------------
104139// Helper functions
105140// ----------------------------------------------
0 commit comments