Refactor error handling
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,14 +12,13 @@ import (
|
|||||||
func (cfg *apiConfig) handlerNotesGet(w http.ResponseWriter, r *http.Request, user database.User) {
|
func (cfg *apiConfig) handlerNotesGet(w http.ResponseWriter, r *http.Request, user database.User) {
|
||||||
posts, err := cfg.DB.GetNotesForUser(r.Context(), user.ID)
|
posts, err := cfg.DB.GetNotesForUser(r.Context(), user.ID)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
postsResp, err := databasePostsToPosts(posts)
|
postsResp, err := databasePostsToPosts(posts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
respondWithError(w, http.StatusInternalServerError, "Couldn't convert posts", err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't convert posts")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +33,7 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request,
|
|||||||
params := parameters{}
|
params := parameters{}
|
||||||
err := decoder.Decode(¶ms)
|
err := decoder.Decode(¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
|
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,20 +46,19 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request,
|
|||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't create note")
|
respondWithError(w, http.StatusInternalServerError, "Couldn't create note", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
note, err := cfg.DB.GetNote(r.Context(), id)
|
note, err := cfg.DB.GetNote(r.Context(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusNotFound, "Couldn't get note")
|
respondWithError(w, http.StatusNotFound, "Couldn't get note", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
noteResp, err := databaseNoteToNote(note)
|
noteResp, err := databaseNoteToNote(note)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
respondWithError(w, http.StatusInternalServerError, "Couldn't convert note", err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't convert note")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -21,13 +20,13 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request)
|
|||||||
params := parameters{}
|
params := parameters{}
|
||||||
err := decoder.Decode(¶ms)
|
err := decoder.Decode(¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
|
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
apiKey, err := generateRandomSHA256Hash()
|
apiKey, err := generateRandomSHA256Hash()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey")
|
respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,22 +38,19 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request)
|
|||||||
ApiKey: apiKey,
|
ApiKey: apiKey,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
respondWithError(w, http.StatusInternalServerError, "Couldn't create user", err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't create user")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := cfg.DB.GetUser(r.Context(), apiKey)
|
user, err := cfg.DB.GetUser(r.Context(), apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
respondWithError(w, http.StatusInternalServerError, "Couldn't get user", err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't get user")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userResp, err := databaseUserToUser(user)
|
userResp, err := databaseUserToUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user", err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
respondWithJSON(w, http.StatusCreated, userResp)
|
respondWithJSON(w, http.StatusCreated, userResp)
|
||||||
@@ -75,8 +71,7 @@ func (cfg *apiConfig) handlerUsersGet(w http.ResponseWriter, r *http.Request, us
|
|||||||
|
|
||||||
userResp, err := databaseUserToUser(user)
|
userResp, err := databaseUserToUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user", err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
json.go
5
json.go
@@ -6,7 +6,10 @@ import (
|
|||||||
"net/http"
|
"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 {
|
if code > 499 {
|
||||||
log.Printf("Responding with 5XX error: %s", msg)
|
log.Printf("Responding with 5XX error: %s", msg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ func (cfg *apiConfig) middlewareAuth(handler authedHandler) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
apiKey, err := auth.GetAPIKey(r.Header)
|
apiKey, err := auth.GetAPIKey(r.Header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusUnauthorized, "Couldn't find api key")
|
respondWithError(w, http.StatusUnauthorized, "Couldn't find api key", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := cfg.DB.GetUser(r.Context(), apiKey)
|
user, err := cfg.DB.GetUser(r.Context(), apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusNotFound, "Couldn't get user")
|
respondWithError(w, http.StatusNotFound, "Couldn't get user", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user