From 2a2dc863bd27420f416a8eb38e5019a038b58768 Mon Sep 17 00:00:00 2001 From: waseem-medhat Date: Thu, 13 Mar 2025 15:10:43 +0200 Subject: [PATCH] Refactor error handling --- handler_notes.go | 15 ++++++--------- handler_user.go | 17 ++++++----------- json.go | 5 ++++- middleware_auth.go | 4 ++-- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/handler_notes.go b/handler_notes.go index 03aaf51..85a8e34 100644 --- a/handler_notes.go +++ b/handler_notes.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "log" "net/http" "time" @@ -13,14 +12,13 @@ import ( func (cfg *apiConfig) handlerNotesGet(w http.ResponseWriter, r *http.Request, user database.User) { posts, err := cfg.DB.GetNotesForUser(r.Context(), user.ID) if err != nil { - respondWithError(w, http.StatusInternalServerError, "Couldn't get posts for user") + respondWithError(w, http.StatusInternalServerError, "Couldn't get posts for user", err) return } postsResp, err := databasePostsToPosts(posts) if err != nil { - log.Println(err) - respondWithError(w, http.StatusInternalServerError, "Couldn't convert posts") + respondWithError(w, http.StatusInternalServerError, "Couldn't convert posts", err) return } @@ -35,7 +33,7 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request, params := parameters{} err := decoder.Decode(¶ms) if err != nil { - respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters") + respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err) return } @@ -48,20 +46,19 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request, UserID: user.ID, }) if err != nil { - respondWithError(w, http.StatusInternalServerError, "Couldn't create note") + respondWithError(w, http.StatusInternalServerError, "Couldn't create note", err) return } note, err := cfg.DB.GetNote(r.Context(), id) if err != nil { - respondWithError(w, http.StatusNotFound, "Couldn't get note") + respondWithError(w, http.StatusNotFound, "Couldn't get note", err) return } noteResp, err := databaseNoteToNote(note) if err != nil { - log.Println(err) - respondWithError(w, http.StatusInternalServerError, "Couldn't convert note") + respondWithError(w, http.StatusInternalServerError, "Couldn't convert note", err) return } diff --git a/handler_user.go b/handler_user.go index a40dfc7..d53d431 100644 --- a/handler_user.go +++ b/handler_user.go @@ -5,7 +5,6 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" - "log" "net/http" "time" @@ -21,13 +20,13 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request) params := parameters{} err := decoder.Decode(¶ms) if err != nil { - respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters") + respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err) return } apiKey, err := generateRandomSHA256Hash() if err != nil { - respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey") + respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey", err) return } @@ -39,22 +38,19 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request) ApiKey: apiKey, }) if err != nil { - log.Println(err) - respondWithError(w, http.StatusInternalServerError, "Couldn't create user") + respondWithError(w, http.StatusInternalServerError, "Couldn't create user", err) return } user, err := cfg.DB.GetUser(r.Context(), apiKey) if err != nil { - log.Println(err) - respondWithError(w, http.StatusInternalServerError, "Couldn't get user") + respondWithError(w, http.StatusInternalServerError, "Couldn't get user", err) return } userResp, err := databaseUserToUser(user) if err != nil { - log.Println(err) - respondWithError(w, http.StatusInternalServerError, "Couldn't convert user") + respondWithError(w, http.StatusInternalServerError, "Couldn't convert user", err) return } respondWithJSON(w, http.StatusCreated, userResp) @@ -75,8 +71,7 @@ func (cfg *apiConfig) handlerUsersGet(w http.ResponseWriter, r *http.Request, us userResp, err := databaseUserToUser(user) if err != nil { - log.Println(err) - respondWithError(w, http.StatusInternalServerError, "Couldn't convert user") + respondWithError(w, http.StatusInternalServerError, "Couldn't convert user", err) return } diff --git a/json.go b/json.go index e346ef4..1e6e798 100644 --- a/json.go +++ b/json.go @@ -6,7 +6,10 @@ import ( "net/http" ) -func respondWithError(w http.ResponseWriter, code int, msg string) { +func respondWithError(w http.ResponseWriter, code int, msg string, logErr error) { + if logErr != nil { + log.Println(logErr) + } if code > 499 { log.Printf("Responding with 5XX error: %s", msg) } diff --git a/middleware_auth.go b/middleware_auth.go index f6a358e..6cbe03f 100644 --- a/middleware_auth.go +++ b/middleware_auth.go @@ -13,13 +13,13 @@ func (cfg *apiConfig) middlewareAuth(handler authedHandler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { apiKey, err := auth.GetAPIKey(r.Header) if err != nil { - respondWithError(w, http.StatusUnauthorized, "Couldn't find api key") + respondWithError(w, http.StatusUnauthorized, "Couldn't find api key", err) return } user, err := cfg.DB.GetUser(r.Context(), apiKey) if err != nil { - respondWithError(w, http.StatusNotFound, "Couldn't get user") + respondWithError(w, http.StatusNotFound, "Couldn't get user", err) return }