Skip to content
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

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions internal/bookmarks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

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

if err != nil {
 stuff;
}

log.Printf("%v", err)
writeStatus(w, http.StatusBadRequest)
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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),
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The 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)

Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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)
}

}
Expand Down
24 changes: 8 additions & 16 deletions internal/db/manager.go
Copy link
Member

Choose a reason for hiding this comment

The 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

Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,28 @@ func scanNotes(rows *sql.Rows) []*Note {
return notes
}

func (m *Manager) GetBookmarks() []*Bookmark {
func (m *Manager) GetBookmarks() ([]*Bookmark, error) {
var rows *sql.Rows
var err error
rows, err = m.DB.Query(findBookmarksSql)
if err != nil {
log.Printf("%v\n", err)
}
return scanBookmarks(rows)
return scanBookmarks(rows), err
}

func (m *Manager) GetBookmarksByUserID(userId int) []*Bookmark {
func (m *Manager) GetBookmarksByUserID(userId int) ([]*Bookmark, error) {
var rows *sql.Rows
var err error
rows, err = m.DB.Query(findBookmarksByUserIDSql, userId)
if err != nil {
log.Printf("%v\n", err)
return nil, err
}
return scanBookmarks(rows)
return scanBookmarks(rows), err
}

func (m *Manager) GetBookmarkByID(bookmarkId int) *Bookmark {
func (m *Manager) GetBookmarkByID(bookmarkId int) (*Bookmark, error) {
row := m.DB.QueryRow(findBookmarksByIDSql, bookmarkId)
return scanBookmark(row)
}
Expand All @@ -223,19 +224,10 @@ func scanBookmarks(rows *sql.Rows) []*Bookmark {
return bookmarks
}

func scanBookmark(row *sql.Row) *Bookmark {
func scanBookmark(row *sql.Row) (*Bookmark, error) {
var bookmark Bookmark
err := row.Scan(&bookmark.Id, &bookmark.Order, &bookmark.UserID, &bookmark.FolderID, &bookmark.ServiceID, &bookmark.ResourceID)
if err != nil {
switch err {
case sql.ErrNoRows:
fmt.Println("No rows were returned!")
return nil
default:
panic(err)
}
}
return &bookmark
return &bookmark, err
}

func (m *Manager) GetAddressesByServiceID(serviceId int) []*Address {
Expand Down
Loading