Merge pull request #37 from bitcodr/improve-error-handling

Improve error handling
This commit is contained in:
Lasse Martin Jakobsen 2020-11-09 09:24:21 +01:00 committed by GitHub
commit 693d670f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -698,7 +698,7 @@ By simply representing the error as a variable (`ErrItemNotFound`), we've ensure
func GetItemHandler(w http.ReponseWriter, r http.Request) {
item, err := clean.GetItem("123")
if err != nil {
if err == clean.ErrItemNotFound {
if errors.Is(err, clean.ErrItemNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
@ -794,7 +794,7 @@ And our HTTP handler function can then be refactored to check for a specific err
func GetItemHandler(w http.ReponseWriter, r http.Request) {
item, err := clean.GetItem("123")
if err != nil {
if err.Type() == clean.ErrItemNotFound {
if errors.Is(err.Type(), clean.ErrItemNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}