improve charts and logs
This commit is contained in:
@@ -77,23 +77,34 @@ func (o *OpenMeteo) Fetch(ctxDone <-chan struct{}, site Site, model string) (*Fo
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
const maxLogBody = int64(65536)
|
||||
bodyBuf, _ := io.ReadAll(io.LimitReader(resp.Body, maxLogBody+1))
|
||||
truncated := int64(len(bodyBuf)) > maxLogBody
|
||||
if truncated {
|
||||
bodyBuf = bodyBuf[:maxLogBody]
|
||||
}
|
||||
if truncated {
|
||||
log.Printf("open-meteo response status=%d bytes=%d truncated=true body=%s", resp.StatusCode, len(bodyBuf), string(bodyBuf))
|
||||
} else {
|
||||
log.Printf("open-meteo response status=%d bytes=%d body=%s", resp.StatusCode, len(bodyBuf), string(bodyBuf))
|
||||
}
|
||||
|
||||
if resp.StatusCode/100 != 2 {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
var apiErr struct {
|
||||
Error bool `json:"error"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &apiErr); err == nil && apiErr.Reason != "" {
|
||||
if err := json.Unmarshal(bodyBuf, &apiErr); err == nil && apiErr.Reason != "" {
|
||||
return nil, fmt.Errorf("open-meteo HTTP %d: %s", resp.StatusCode, apiErr.Reason)
|
||||
}
|
||||
if len(body) > 0 {
|
||||
return nil, fmt.Errorf("open-meteo HTTP %d: %s", resp.StatusCode, string(body))
|
||||
if len(bodyBuf) > 0 {
|
||||
return nil, fmt.Errorf("open-meteo HTTP %d: %s", resp.StatusCode, string(bodyBuf))
|
||||
}
|
||||
return nil, fmt.Errorf("open-meteo HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var raw map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil {
|
||||
if err := json.Unmarshal(bodyBuf, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user