From c29dd9e6fba28fd08f50f428d1da39d9a2ed1319 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 15 Apr 2024 17:35:02 +0200 Subject: [PATCH] better error handling and logging --- main.go | 15 +++++++-------- request.go | 29 +++++++++++++---------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index bf1e5fc..5d20287 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "log" "github.com/buger/goterm" "github.com/go-resty/resty/v2" @@ -10,7 +9,7 @@ import ( ) func output(client *resty.Client, url string, width, height int, arr [][]Cell) { - goterm.MoveCursor(1, 2) + goterm.MoveCursor(1, 4) // Prepare instructions for matrix instructions := make([]interface{}, 0) // Append all live cells as pixel instructions @@ -37,11 +36,8 @@ func output(client *resty.Client, url string, width, height int, arr [][]Cell) { func main() { const FPS = 10 - // Load .env file if it exists - err := godotenv.Load(".env") - if err != nil { - log.Fatal("Error loading .env file") - } + // Try loading .env file if it exists + godotenv.Load(".env") // Load env server data url, width, height := loadMatrixData() @@ -50,7 +46,10 @@ func main() { // Initialize resty client client := resty.New() // Initialize pixel color - sendRequest(client, url, []interface{}{Color{R: 255, G: 255, B: 255, Endpoint: COLOR}}) + err := sendRequest(client, url, []interface{}{Color{R: 255, G: 255, B: 255, Endpoint: COLOR}}) + if err != nil { + goterm.Println(err) + } // Run Game of Life done := setup(func(c [][]Cell) { diff --git a/request.go b/request.go index f80239f..b32b938 100644 --- a/request.go +++ b/request.go @@ -5,13 +5,12 @@ import ( "encoding/json" "errors" "fmt" - "log" "mime/multipart" "github.com/go-resty/resty/v2" ) -func createInsructions(marshal []byte) (*bytes.Buffer, string) { +func createInsructions(marshal []byte) (*bytes.Buffer, string, error) { // Create a new form data object body := &bytes.Buffer{} writer := multipart.NewWriter(body) @@ -19,29 +18,27 @@ func createInsructions(marshal []byte) (*bytes.Buffer, string) { // Add the "instructions" key with the value of marshal part, err := writer.CreateFormField("instructions") if err != nil { - log.Fatal(err) - return nil, "" + return nil, "", err } part.Write(marshal) // Close the multipart writer writer.Close() - return body, writer.FormDataContentType() + return body, writer.FormDataContentType(), nil } -func sendRequest(client *resty.Client, url string, ins []interface{}) { +func sendRequest(client *resty.Client, url string, ins []interface{}) error { // Manually marshal instructions marshal, err := json.Marshal(ins) if err != nil { - log.Fatal(err) - return + return err } // Create instructions form data - req, content := createInsructions(marshal) - if req == nil { - return + req, content, err := createInsructions(marshal) + if err != nil { + return err } // Build and send request @@ -52,20 +49,20 @@ func sendRequest(client *resty.Client, url string, ins []interface{}) { // Error handling if err != nil { - log.Fatal(err) - return + return err } // Unmarshal response var data Response err = json.Unmarshal(res.Body(), &data) if err != nil { - log.Fatal(err) - return + return err } // Print response if !data.Success { - log.Fatal(errors.New("Remote server responded unsuccessfully!")) + return errors.New("Remote server responded unsuccessfully!") } + + return nil }