better error handling and logging

This commit is contained in:
Baipyrus 2024-04-15 17:35:02 +02:00
parent 6eeba8f9d0
commit c29dd9e6fb
2 changed files with 20 additions and 24 deletions

15
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"github.com/buger/goterm" "github.com/buger/goterm"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -10,7 +9,7 @@ import (
) )
func output(client *resty.Client, url string, width, height int, arr [][]Cell) { func output(client *resty.Client, url string, width, height int, arr [][]Cell) {
goterm.MoveCursor(1, 2) goterm.MoveCursor(1, 4)
// Prepare instructions for matrix // Prepare instructions for matrix
instructions := make([]interface{}, 0) instructions := make([]interface{}, 0)
// Append all live cells as pixel instructions // 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() { func main() {
const FPS = 10 const FPS = 10
// Load .env file if it exists // Try loading .env file if it exists
err := godotenv.Load(".env") godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
// Load env server data // Load env server data
url, width, height := loadMatrixData() url, width, height := loadMatrixData()
@ -50,7 +46,10 @@ func main() {
// Initialize resty client // Initialize resty client
client := resty.New() client := resty.New()
// Initialize pixel color // 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 // Run Game of Life
done := setup(func(c [][]Cell) { done := setup(func(c [][]Cell) {

View File

@ -5,13 +5,12 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"log"
"mime/multipart" "mime/multipart"
"github.com/go-resty/resty/v2" "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 // Create a new form data object
body := &bytes.Buffer{} body := &bytes.Buffer{}
writer := multipart.NewWriter(body) writer := multipart.NewWriter(body)
@ -19,29 +18,27 @@ func createInsructions(marshal []byte) (*bytes.Buffer, string) {
// Add the "instructions" key with the value of marshal // Add the "instructions" key with the value of marshal
part, err := writer.CreateFormField("instructions") part, err := writer.CreateFormField("instructions")
if err != nil { if err != nil {
log.Fatal(err) return nil, "", err
return nil, ""
} }
part.Write(marshal) part.Write(marshal)
// Close the multipart writer // Close the multipart writer
writer.Close() 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 // Manually marshal instructions
marshal, err := json.Marshal(ins) marshal, err := json.Marshal(ins)
if err != nil { if err != nil {
log.Fatal(err) return err
return
} }
// Create instructions form data // Create instructions form data
req, content := createInsructions(marshal) req, content, err := createInsructions(marshal)
if req == nil { if err != nil {
return return err
} }
// Build and send request // Build and send request
@ -52,20 +49,20 @@ func sendRequest(client *resty.Client, url string, ins []interface{}) {
// Error handling // Error handling
if err != nil { if err != nil {
log.Fatal(err) return err
return
} }
// Unmarshal response // Unmarshal response
var data Response var data Response
err = json.Unmarshal(res.Body(), &data) err = json.Unmarshal(res.Body(), &data)
if err != nil { if err != nil {
log.Fatal(err) return err
return
} }
// Print response // Print response
if !data.Success { if !data.Success {
log.Fatal(errors.New("Remote server responded unsuccessfully!")) return errors.New("Remote server responded unsuccessfully!")
} }
return nil
} }