mirror of
https://github.com/Luzifer/mercedes-byocar-exporter.git
synced 2024-11-08 14:00:09 +00:00
Fix: Gracefully handle data not being available for query type
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
50c40ed68a
commit
d1761c5975
2 changed files with 30 additions and 26 deletions
50
fetcher.go
50
fetcher.go
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/Luzifer/mercedes-byocar-exporter/internal/mercedes"
|
"github.com/Luzifer/mercedes-byocar-exporter/internal/mercedes"
|
||||||
|
@ -19,39 +21,35 @@ func runFetcher(mc mercedes.Client, vehicleID string) {
|
||||||
logger.Info("fetching data")
|
logger.Info("fetching data")
|
||||||
|
|
||||||
s1, err := mc.GetPayAsYouDriveInsurance(cfg.VehicleID[0])
|
s1, err := mc.GetPayAsYouDriveInsurance(cfg.VehicleID[0])
|
||||||
if err != nil {
|
handleMetricsEntries(logger, "pay-as-you-go", err, func() { enabledExporters.SetPayAsYouGo(vehicleID, s1) })
|
||||||
logger.WithError(err).Error("fetching pay-as-you-go data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enabledExporters.SetPayAsYouGo(vehicleID, s1)
|
|
||||||
|
|
||||||
s2, err := mc.GetFuelStatus(cfg.VehicleID[0])
|
s2, err := mc.GetFuelStatus(cfg.VehicleID[0])
|
||||||
if err != nil {
|
handleMetricsEntries(logger, "fuel-status", err, func() { enabledExporters.SetFuelStatus(vehicleID, s2) })
|
||||||
logger.WithError(err).Error("fetching fuel-status data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enabledExporters.SetFuelStatus(vehicleID, s2)
|
|
||||||
|
|
||||||
s3, err := mc.GetVehicleStatus(cfg.VehicleID[0])
|
s3, err := mc.GetVehicleStatus(cfg.VehicleID[0])
|
||||||
if err != nil {
|
handleMetricsEntries(logger, "vehicle-status", err, func() { enabledExporters.SetVehicleStatus(vehicleID, s3) })
|
||||||
logger.WithError(err).Error("fetching vehicle-status data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enabledExporters.SetVehicleStatus(vehicleID, s3)
|
|
||||||
|
|
||||||
s4, err := mc.GetLockStatus(cfg.VehicleID[0])
|
s4, err := mc.GetLockStatus(cfg.VehicleID[0])
|
||||||
if err != nil {
|
handleMetricsEntries(logger, "lock-status", err, func() { enabledExporters.SetLockStatus(vehicleID, s4) })
|
||||||
logger.WithError(err).Error("fetching lock-status data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enabledExporters.SetLockStatus(vehicleID, s4)
|
|
||||||
|
|
||||||
s5, err := mc.GetElectricStatus(cfg.VehicleID[0])
|
s5, err := mc.GetElectricStatus(cfg.VehicleID[0])
|
||||||
if err != nil {
|
handleMetricsEntries(logger, "electric-status", err, func() { enabledExporters.SetElectricStatus(vehicleID, s5) })
|
||||||
logger.WithError(err).Error("fetching electric-status data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
enabledExporters.SetElectricStatus(vehicleID, s5)
|
|
||||||
|
|
||||||
logger.Info("data updated successfully")
|
logger.Info("data updated")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleMetricsEntries(logger *logrus.Entry, dataType string, err error, submit func()) {
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
submit()
|
||||||
|
|
||||||
|
case errors.Is(err, mercedes.ErrNoDataAvailable):
|
||||||
|
logger.Warnf("%s data is not available", dataType)
|
||||||
|
return
|
||||||
|
|
||||||
|
default:
|
||||||
|
logger.WithError(err).Errorf("fetching %s data", dataType)
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,8 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrNoDataAvailable = errors.New("no data available for this endpoint")
|
||||||
|
|
||||||
var _ Client = (*APIClient)(nil)
|
var _ Client = (*APIClient)(nil)
|
||||||
|
|
||||||
func New(clientID, clientSecret string, creds credential.Store) *APIClient {
|
func New(clientID, clientSecret string, creds credential.Store) *APIClient {
|
||||||
|
@ -202,6 +204,10 @@ func (a APIClient) request(path string, output any) error {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusNoContent {
|
||||||
|
return ErrNoDataAvailable
|
||||||
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue