26
26
weatherDataStore sync.Map
27
27
defaultLocationMu sync.RWMutex
28
28
defaultLocation string
29
+ httpClient = & http.Client {
30
+ Timeout : 10 * time .Second ,
31
+ }
29
32
)
30
33
31
34
type Location struct {
@@ -248,9 +251,7 @@ func (w *Location) fetchWeatherData(ctx context.Context, endpoint string, result
248
251
qLog .Set ("appid" , "REDACTED" )
249
252
apiURLForLog .RawQuery = qLog .Encode ()
250
253
251
- client := & http.Client {
252
- Timeout : time .Second * 10 ,
253
- }
254
+ client := httpClient
254
255
req , err := http .NewRequestWithContext (ctx , http .MethodGet , apiURL .String (), nil )
255
256
if err != nil {
256
257
log .Error (err )
@@ -277,8 +278,13 @@ func (w *Location) fetchWeatherData(ctx context.Context, endpoint string, result
277
278
defer res .Body .Close ()
278
279
279
280
if res .StatusCode < 200 || res .StatusCode >= 300 {
280
- err = fmt .Errorf ("unexpected status code: %d" , res .StatusCode )
281
- log .Error (err )
281
+ bodyPreview , _ := io .ReadAll (io .LimitReader (res .Body , 1024 ))
282
+ err = fmt .Errorf ("unexpected status code: %d, body: %s" ,
283
+ res .StatusCode , strings .TrimSpace (string (bodyPreview )))
284
+ log .Error ("OpenWeatherMap API error" ,
285
+ "url" , apiURLForLog .String (),
286
+ "status" , res .StatusCode ,
287
+ "body" , string (bodyPreview ))
282
288
return err
283
289
}
284
290
@@ -317,7 +323,7 @@ func (w *Location) updateForecast(ctx context.Context) (Location, error) {
317
323
if err != nil {
318
324
return * w , err
319
325
}
320
- w .Forecast = processForecast (newForecast , w .Weather . Timezone )
326
+ w .Forecast = processForecast (newForecast , w .Timezone )
321
327
return * w , nil
322
328
}
323
329
0 commit comments