From 237f7a29075999b06c3d64f396a0ef64fe375b6e Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Tue, 9 Jul 2019 16:41:35 +0200 Subject: [PATCH] rebuilt error handling --- fuelprices.go | 32 +++++++++++++++++++-------- functions.go | 60 ++++++++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/fuelprices.go b/fuelprices.go index 57749ac..bb29407 100644 --- a/fuelprices.go +++ b/fuelprices.go @@ -2,28 +2,42 @@ package main import ( "flag" + "log" ) func main() { var configpath string var fpc FuelPricesConfig var err error - - flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section") - flag.Parse() - GetConfig(configpath, &fpc) - var szip Srcfile var output []byte + flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section") + flag.Parse() + + err = GetConfig(configpath, &fpc) + if err != nil { + log.Fatal(err) + } + err = DownloadFile(&fpc, &szip) - HandleFatalError(err) + if err != nil { + log.Fatal(err) + } err = ExtractZip(&fpc, &szip, &output) - HandleFatalError(err) + if err != nil { + log.Fatal(err) + } var prices *[]Price - GetPrices(&prices, &fpc, &output) + err = GetPrices(&prices, &fpc, &output) + if err != nil { + log.Fatal(err) + } - SendToInflux(&fpc, prices) + err = SendToInflux(&fpc, prices) + if err != nil { + log.Fatal(err) + } } diff --git a/functions.go b/functions.go index 7c3fe5d..34bcc7e 100644 --- a/functions.go +++ b/functions.go @@ -28,7 +28,9 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error { flag.Usage = Usage config, err := ini.Load(configfile) - HandleFatalError(err) + if err != nil { + return err + } var fpc FuelPricesConfig @@ -41,10 +43,8 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error { influxdbSection := config.Section("influxdb") fpc.InfluxHost = influxdbSection.Key("hostname").String() fpc.InfluxPort, err = influxdbSection.Key("port").Int() - HandleError(err) fpc.InfluxUser = influxdbSection.Key("username").String() fpc.InfluxPass = influxdbSection.Key("password").String() - HandleError(err) fpc.InfluxDB = influxdbSection.Key("database").String() @@ -53,7 +53,11 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error { *fuelpricesconfig = fpc - return err + if err != nil { + return err + } + + return nil } // DownloadFile fetch file from webserver @@ -66,23 +70,29 @@ func DownloadFile(fpc *FuelPricesConfig, szip *Srcfile) error { }} //resp, err := http.Get(url) resp, err := c.Get(fpc.RemoteURL) - HandleError(err) + if err != nil { + return err + } defer resp.Body.Close() szip.Content, err = ioutil.ReadAll(resp.Body) - HandleError(err) + if err != nil { + return err + } time.Sleep(pollTo) - return err + return nil } // ExtractZip get the XML file to be processed func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error { zipfile, err := zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content))) - HandleFatalError(err) + if err != nil { + return err + } for _, f := range zipfile.File { if f.Name == fpc.RemoteFilename { @@ -104,9 +114,12 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error { var pr []Price var xml *xmlquery.Node + f := bytes.NewReader(*output) xml, err := xmlquery.Parse(f) - HandleError(err) + if err != nil { + return err + } for _, station := range fpc.Pos { for _, fuel := range fpc.Types { @@ -134,18 +147,22 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error { } // SendToInflux sends time series data to influxdb -func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) { +func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) error { httpClient, err := client.NewHTTPClient(client.HTTPConfig{ Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort), Username: fpc.InfluxUser, Password: fpc.InfluxPass, }) - HandleFatalError(err) + if err != nil { + return err + } bp, err := client.NewBatchPoints(client.BatchPointsConfig{ Database: fpc.InfluxDB, }) - HandleFatalError(err) + if err != nil { + return err + } for _, p := range *prices { @@ -163,23 +180,12 @@ func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) { bp.AddPoint(point) err = httpClient.Write(bp) - HandleError(err) + if err != nil { + return err + } } -} -// HandleError handles errors -func HandleError(err error) { - if err != nil { - log.Println(err) - } -} - -// HandleFatalError handles fatal errors -func HandleFatalError(err error) { - if err != nil { - log.Fatal(err) - os.Exit(2) - } + return nil } // Usage displays possible arguments