Merge pull request #1266 from bootdotdev/wm_refactor_error_handling

Refactor error handling
This commit is contained in:
Waseem Medhat
2025-03-14 20:02:57 +02:00
committed by GitHub
4 changed files with 18 additions and 23 deletions

View File

@@ -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(&params)
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
}

View File

@@ -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(&params)
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
}

View File

@@ -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)
}

View File

@@ -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
}