Skip to content

Commit 71464cf

Browse files
committed
Handle user not found error and only return user's public info
1 parent b99c047 commit 71464cf

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

handlers/user.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,31 @@ import (
66
"net/http"
77

88
"github.com/gorilla/mux"
9+
"github.com/play-with-docker/play-with-docker/storage"
910
)
1011

12+
type PublicUserInfo struct {
13+
Id string `json:"id"`
14+
Avatar string `json:"avatar"`
15+
Name string `json:"name"`
16+
}
17+
1118
func GetUser(rw http.ResponseWriter, req *http.Request) {
1219
vars := mux.Vars(req)
1320
userId := vars["userId"]
1421

1522
u, err := core.UserGet(userId)
1623
if err != nil {
24+
if storage.NotFound(err) {
25+
log.Printf("User with id %s was not found\n", userId)
26+
rw.WriteHeader(http.StatusNotFound)
27+
return
28+
}
1729
log.Println(err)
1830
rw.WriteHeader(http.StatusInternalServerError)
1931
return
2032
}
2133

22-
json.NewEncoder(rw).Encode(u)
34+
pui := PublicUserInfo{Id: u.Id, Avatar: u.Avatar, Name: u.Name}
35+
json.NewEncoder(rw).Encode(pui)
2336
}

0 commit comments

Comments
 (0)