oauth_github_callback.go: add better error handling

This commit is contained in:
Adhityaa Chandrasekar 2019-02-22 21:57:13 -05:00
parent 3e5c1c2656
commit 95093326e0

View File

@ -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) 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() defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body) contents, err := ioutil.ReadAll(resp.Body)
@ -80,6 +84,23 @@ func githubCallbackHandler(w http.ResponseWriter, r *http.Request) {
email = user["email"].(string) 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) c, err := commenterGetByEmail("github", email)
if err != nil && err != errorNoSuchCommenter { if err != nil && err != errorNoSuchCommenter {
fmt.Fprintf(w, "Error: %s", err.Error()) 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? // TODO: in case of returning users, update the information we have on record?
if err == errorNoSuchCommenter { if err == errorNoSuchCommenter {
var link string commenterHex, err = commenterNew(email, name, link, photo, "github", "")
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", "")
if err != nil { if err != nil {
fmt.Fprintf(w, "Error: %s", err.Error()) fmt.Fprintf(w, "Error: %s", err.Error())
return return