-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed error handling for bookmarks manager #137
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,24 @@ func (m *Manager) Get(w http.ResponseWriter, r *http.Request) { | |
userId := r.URL.Query().Get("user_id") | ||
|
||
if userId != "" { | ||
iUserId, _ := strconv.Atoi(userId) | ||
dbBookmarks = m.DbClient.GetBookmarksByUserID(iUserId) | ||
iUserId, err := strconv.Atoi(userId) | ||
if (err != nil) { | ||
log.Printf("%v", err) | ||
writeStatus(w, http.StatusBadRequest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a case where we can update the flow to provide an error message json body response There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add a test for any 400 cases |
||
return | ||
} | ||
dbBookmarks, err = m.DbClient.GetBookmarksByUserID(iUserId) | ||
if (err != nil) { | ||
writeStatus(w, http.StatusInternalServerError) | ||
return | ||
} | ||
} else { | ||
dbBookmarks = m.DbClient.GetBookmarks() | ||
var err error | ||
dbBookmarks, err = m.DbClient.GetBookmarks() | ||
if (err != nil) { | ||
writeStatus(w, http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
response := Bookmarks{ | ||
Bookmarks: FromDBTypeArray(dbBookmarks), | ||
|
@@ -46,9 +60,15 @@ func (m *Manager) GetByID(w http.ResponseWriter, r *http.Request) { | |
id, err := strconv.Atoi(chi.URLParam(r, "id")) | ||
if err != nil { | ||
log.Printf("%v", err) | ||
writeStatus(w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
dbBookmark := m.DbClient.GetBookmarkByID(id) | ||
dbBookmark, err := m.DbClient.GetBookmarkByID(id) | ||
if (err != nil) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to log the error here too, just the same flow as other cases |
||
writeStatus(w, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
response := FromDBType(dbBookmark) | ||
|
||
|
@@ -63,7 +83,9 @@ func (m *Manager) Submit(w http.ResponseWriter, r *http.Request) { | |
bookmark := &Bookmark{} | ||
err := json.Unmarshal(body, bookmark) | ||
if err != nil { | ||
writeStatus(w, http.StatusInternalServerError) | ||
log.Printf("%v", err) | ||
writeStatus(w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
dbBookmark := &db.Bookmark{ | ||
|
@@ -78,6 +100,7 @@ func (m *Manager) Submit(w http.ResponseWriter, r *http.Request) { | |
if err != nil { | ||
log.Print(err) | ||
writeStatus(w, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
writeStatus(w, http.StatusCreated) | ||
|
@@ -92,7 +115,9 @@ func (m *Manager) Update(w http.ResponseWriter, r *http.Request) { | |
bookmark := &Bookmark{} | ||
err := json.Unmarshal(body, bookmark) | ||
if err != nil { | ||
writeStatus(w, http.StatusInternalServerError) | ||
log.Printf("%v", err) | ||
writeStatus(w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
dbBookmark := &db.Bookmark{ | ||
|
@@ -108,6 +133,7 @@ func (m *Manager) Update(w http.ResponseWriter, r *http.Request) { | |
if err != nil { | ||
log.Print(err) | ||
writeStatus(w, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
writeStatus(w, http.StatusCreated) | ||
|
@@ -119,11 +145,14 @@ func (m *Manager) DeleteByID(w http.ResponseWriter, r *http.Request) { | |
bookmarkId, err := strconv.Atoi(chi.URLParam(r, "id")) | ||
if err != nil { | ||
log.Printf("%v", err) | ||
writeStatus(w, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = m.DbClient.DeleteBookmarkByID(bookmarkId) | ||
if err != nil { | ||
log.Printf("%v", err) | ||
writeStatus(w, http.StatusInternalServerError) | ||
} | ||
|
||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense, we do want to bubble up errors, it was a mistake of me to not do that originally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont need to wrap this, normal to just do