From 71464cffee772d35f41c3b7ddf3dcab6ce23f2b0 Mon Sep 17 00:00:00 2001 From: "Jonathan Leibiusky (@xetorthio)" Date: Tue, 17 Oct 2017 12:02:35 +0200 Subject: [PATCH] Handle user not found error and only return user's public info --- handlers/user.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/handlers/user.go b/handlers/user.go index 002281a..577adc9 100644 --- a/handlers/user.go +++ b/handlers/user.go @@ -6,18 +6,31 @@ import ( "net/http" "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) { vars := mux.Vars(req) userId := vars["userId"] u, err := core.UserGet(userId) 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) rw.WriteHeader(http.StatusInternalServerError) return } - json.NewEncoder(rw).Encode(u) + pui := PublicUserInfo{Id: u.Id, Avatar: u.Avatar, Name: u.Name} + json.NewEncoder(rw).Encode(pui) }