From 95093326e0f64b1de5453a7bbaf2a4c22a235ffc Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Fri, 22 Feb 2019 21:57:13 -0500 Subject: [PATCH] oauth_github_callback.go: add better error handling --- api/oauth_github_callback.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/api/oauth_github_callback.go b/api/oauth_github_callback.go index d00e78a..e6d8d43 100644 --- a/api/oauth_github_callback.go +++ b/api/oauth_github_callback.go @@ -57,6 +57,10 @@ func githubCallbackHandler(w http.ResponseWriter, r *http.Request) { } resp, err := http.Get("https://api.github.com/user?access_token=" + token.AccessToken) + if err != nil { + fmt.Fprintf(w, "Error: %s", err.Error()) + return + } defer resp.Body.Close() contents, err := ioutil.ReadAll(resp.Body) @@ -80,6 +84,23 @@ func githubCallbackHandler(w http.ResponseWriter, r *http.Request) { email = user["email"].(string) } + if user["name"] == nil { + fmt.Fprintf(w, "Error: no name returned by Github") + return + } + + name := user["name"].(string) + + link := "undefined" + if user["html_url"] != nil { + link = user["html_url"].(string) + } + + photo := "undefined" + if user["avatar_url"] != nil { + photo = user["avatar_url"].(string) + } + c, err := commenterGetByEmail("github", email) if err != nil && err != errorNoSuchCommenter { fmt.Fprintf(w, "Error: %s", err.Error()) @@ -90,14 +111,7 @@ func githubCallbackHandler(w http.ResponseWriter, r *http.Request) { // TODO: in case of returning users, update the information we have on record? if err == errorNoSuchCommenter { - var link string - if val, ok := user["html_url"]; ok { - link = val.(string) - } else { - link = "undefined" - } - - commenterHex, err = commenterNew(email, user["name"].(string), link, user["avatar_url"].(string), "github", "") + commenterHex, err = commenterNew(email, name, link, photo, "github", "") if err != nil { fmt.Fprintf(w, "Error: %s", err.Error()) return