Skip to content

Commit a759e37

Browse files
Merge pull request #1266 from bootdotdev/wm_refactor_error_handling
Refactor error handling
2 parents 59115e6 + 2a2dc86 commit a759e37

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

handler_notes.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"encoding/json"
5-
"log"
65
"net/http"
76
"time"
87

@@ -13,14 +12,13 @@ import (
1312
func (cfg *apiConfig) handlerNotesGet(w http.ResponseWriter, r *http.Request, user database.User) {
1413
posts, err := cfg.DB.GetNotesForUser(r.Context(), user.ID)
1514
if err != nil {
16-
respondWithError(w, http.StatusInternalServerError, "Couldn't get posts for user")
15+
respondWithError(w, http.StatusInternalServerError, "Couldn't get posts for user", err)
1716
return
1817
}
1918

2019
postsResp, err := databasePostsToPosts(posts)
2120
if err != nil {
22-
log.Println(err)
23-
respondWithError(w, http.StatusInternalServerError, "Couldn't convert posts")
21+
respondWithError(w, http.StatusInternalServerError, "Couldn't convert posts", err)
2422
return
2523
}
2624

@@ -35,7 +33,7 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request,
3533
params := parameters{}
3634
err := decoder.Decode(&params)
3735
if err != nil {
38-
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
36+
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
3937
return
4038
}
4139

@@ -48,20 +46,19 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request,
4846
UserID: user.ID,
4947
})
5048
if err != nil {
51-
respondWithError(w, http.StatusInternalServerError, "Couldn't create note")
49+
respondWithError(w, http.StatusInternalServerError, "Couldn't create note", err)
5250
return
5351
}
5452

5553
note, err := cfg.DB.GetNote(r.Context(), id)
5654
if err != nil {
57-
respondWithError(w, http.StatusNotFound, "Couldn't get note")
55+
respondWithError(w, http.StatusNotFound, "Couldn't get note", err)
5856
return
5957
}
6058

6159
noteResp, err := databaseNoteToNote(note)
6260
if err != nil {
63-
log.Println(err)
64-
respondWithError(w, http.StatusInternalServerError, "Couldn't convert note")
61+
respondWithError(w, http.StatusInternalServerError, "Couldn't convert note", err)
6562
return
6663
}
6764

handler_user.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/sha256"
66
"encoding/hex"
77
"encoding/json"
8-
"log"
98
"net/http"
109
"time"
1110

@@ -21,13 +20,13 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request)
2120
params := parameters{}
2221
err := decoder.Decode(&params)
2322
if err != nil {
24-
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
23+
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
2524
return
2625
}
2726

2827
apiKey, err := generateRandomSHA256Hash()
2928
if err != nil {
30-
respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey")
29+
respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey", err)
3130
return
3231
}
3332

@@ -39,22 +38,19 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request)
3938
ApiKey: apiKey,
4039
})
4140
if err != nil {
42-
log.Println(err)
43-
respondWithError(w, http.StatusInternalServerError, "Couldn't create user")
41+
respondWithError(w, http.StatusInternalServerError, "Couldn't create user", err)
4442
return
4543
}
4644

4745
user, err := cfg.DB.GetUser(r.Context(), apiKey)
4846
if err != nil {
49-
log.Println(err)
50-
respondWithError(w, http.StatusInternalServerError, "Couldn't get user")
47+
respondWithError(w, http.StatusInternalServerError, "Couldn't get user", err)
5148
return
5249
}
5350

5451
userResp, err := databaseUserToUser(user)
5552
if err != nil {
56-
log.Println(err)
57-
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user")
53+
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user", err)
5854
return
5955
}
6056
respondWithJSON(w, http.StatusCreated, userResp)
@@ -75,8 +71,7 @@ func (cfg *apiConfig) handlerUsersGet(w http.ResponseWriter, r *http.Request, us
7571

7672
userResp, err := databaseUserToUser(user)
7773
if err != nil {
78-
log.Println(err)
79-
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user")
74+
respondWithError(w, http.StatusInternalServerError, "Couldn't convert user", err)
8075
return
8176
}
8277

json.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"net/http"
77
)
88

9-
func respondWithError(w http.ResponseWriter, code int, msg string) {
9+
func respondWithError(w http.ResponseWriter, code int, msg string, logErr error) {
10+
if logErr != nil {
11+
log.Println(logErr)
12+
}
1013
if code > 499 {
1114
log.Printf("Responding with 5XX error: %s", msg)
1215
}

middleware_auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ func (cfg *apiConfig) middlewareAuth(handler authedHandler) http.HandlerFunc {
1313
return func(w http.ResponseWriter, r *http.Request) {
1414
apiKey, err := auth.GetAPIKey(r.Header)
1515
if err != nil {
16-
respondWithError(w, http.StatusUnauthorized, "Couldn't find api key")
16+
respondWithError(w, http.StatusUnauthorized, "Couldn't find api key", err)
1717
return
1818
}
1919

2020
user, err := cfg.DB.GetUser(r.Context(), apiKey)
2121
if err != nil {
22-
respondWithError(w, http.StatusNotFound, "Couldn't get user")
22+
respondWithError(w, http.StatusNotFound, "Couldn't get user", err)
2323
return
2424
}
2525

0 commit comments

Comments
 (0)