From 53b74ec6122c94853f9d43ab2ac1f69efab0886d Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Mon, 18 Mar 2024 21:27:24 -0400 Subject: clean up, add better error handling --- api/internal/controllers/preferences.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'api/internal/controllers/preferences.go') diff --git a/api/internal/controllers/preferences.go b/api/internal/controllers/preferences.go index a1bcf4f..da52e74 100644 --- a/api/internal/controllers/preferences.go +++ b/api/internal/controllers/preferences.go @@ -1,22 +1,27 @@ package controllers import ( - "github.com/gin-gonic/gin" - "net/http" "database/sql" + "net/http" "water/api/internal/database" "water/api/internal/models" + + "github.com/gin-gonic/gin" ) func GetSizes(c *gin.Context) { - db := database.EstablishDBConnection() + db, err := database.EstablishDBConnection() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } defer func(db *sql.DB) { err := db.Close() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) } }(db) - + rows, err := db.Query("SELECT id, size, unit FROM Sizes") if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) -- cgit v1.1