Ensure consistent error handling and improve resource cleanup across widgets and APIs.

This commit is contained in:
2026-01-01 12:10:33 +01:00
parent af5a39ef75
commit 2a66278cae
9 changed files with 90 additions and 48 deletions

View File

@@ -3,6 +3,7 @@ package docker
import (
"ArinDash/util"
"context"
"io"
"strings"
containertypes "github.com/docker/docker/api/types/container"
@@ -34,7 +35,12 @@ func FetchDockerMetrics(ctx context.Context) ([]ContainerMetrics, error) {
if err != nil {
return nil, err
}
defer cli.Close()
defer func(cli *client.Client) {
err := cli.Close()
if err != nil {
return
}
}(cli)
_, err = cli.Ping(ctx)
if err != nil {
@@ -60,7 +66,12 @@ func FetchDockerMetrics(ctx context.Context) ([]ContainerMetrics, error) {
continue
}
func() {
defer stats.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(stats.Body)
var sj containertypes.StatsResponse
if err := util.DecodeJSON(stats.Body, &sj); err != nil {
return
@@ -109,9 +120,7 @@ func cpuPercentFromStats(s containertypes.StatsResponse) float64 {
func memoryFromStats(s containertypes.StatsResponse) (usage, limit uint64, percent float64) {
usage = s.MemoryStats.Usage
limit = s.MemoryStats.Limit
// Optionally discount cached memory if stats present.
if stats := s.MemoryStats.Stats; stats != nil {
// The common Linux approach: usage - cache
if cache, ok := stats["cache"]; ok && cache <= usage {
usage -= cache
}

View File

@@ -11,6 +11,8 @@ import (
const apiBaseURL = "https://newsapi.org/v2/top-headlines"
var forbiddenStrings = []string{"\r", "\\r", "\ufeff", "\u00A0"}
type configFile struct {
News newsConfig
}
@@ -65,7 +67,12 @@ func FetchNews() News {
Status: err.Error(),
}
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return News{
@@ -75,7 +82,7 @@ func FetchNews() News {
news := News{}
err = json.Unmarshal([]byte(strings.ReplaceAll(strings.ReplaceAll(string(respBody), "\r", ""), "\u00A0", "")), &news)
err = json.Unmarshal([]byte(removeForbiddenStrings(string(respBody))), &news)
if err != nil {
log.Fatal(err)
@@ -83,3 +90,11 @@ func FetchNews() News {
return news
}
func removeForbiddenStrings(s string) string {
result := s
for _, forbidden := range forbiddenStrings {
result = strings.ReplaceAll(result, forbidden, "")
}
return result
}

View File

@@ -57,7 +57,12 @@ func FetchWarnings() []Warning {
Errormsg: err.Error(),
})
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return append(make([]Warning, 0), Warning{
@@ -86,7 +91,12 @@ func FetchWarnings() []Warning {
Errormsg: err.Error(),
})
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return append(make([]Warning, 0), Warning{

View File

@@ -41,7 +41,12 @@ func FetchCurrentWeather() CurrentWeather {
ErrorMessage: err.Error(),
}
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return CurrentWeather{

View File

@@ -59,7 +59,12 @@ func (ph *PiHConnector) do(method string, endpoint string, body io.Reader) []byt
if err != nil {
return make([]byte, 0)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
@@ -90,7 +95,12 @@ func Connect() PiHConnector {
if err != nil {
panic(err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {