Handle user not found error and only return user's public info

This commit is contained in:
Jonathan Leibiusky (@xetorthio)
2017-10-17 12:02:35 +02:00
parent b99c047a0c
commit 71464cffee

View File

@@ -6,18 +6,31 @@ import (
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
) )
type PublicUserInfo struct {
Id string `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
}
func GetUser(rw http.ResponseWriter, req *http.Request) { func GetUser(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
userId := vars["userId"] userId := vars["userId"]
u, err := core.UserGet(userId) u, err := core.UserGet(userId)
if err != nil { if err != nil {
if storage.NotFound(err) {
log.Printf("User with id %s was not found\n", userId)
rw.WriteHeader(http.StatusNotFound)
return
}
log.Println(err) log.Println(err)
rw.WriteHeader(http.StatusInternalServerError) rw.WriteHeader(http.StatusInternalServerError)
return return
} }
json.NewEncoder(rw).Encode(u) pui := PublicUserInfo{Id: u.Id, Avatar: u.Avatar, Name: u.Name}
json.NewEncoder(rw).Encode(pui)
} }