From 735ac7ffb3fdb9b7bd9b2fb7bf98f33df3de72b7 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Sat, 20 Dec 2014 21:38:09 -0800 Subject: [PATCH] Better handling of GitHub OAuth errors when sending digest emails. Fail silently if we can't even get an email address, otherwise send an error email to the user. --- app/app.go | 6 ++--- app/retrogit.go | 29 ++++++++++++++++++++++ app/templates/digest-email.html | 21 +--------------- app/templates/github-auth-error-email.html | 16 ++++++++++++ app/templates/shared/email-footer.html | 24 ++++++++++++++++++ 5 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 app/templates/github-auth-error-email.html create mode 100644 app/templates/shared/email-footer.html diff --git a/app/app.go b/app/app.go index c999430..c429ddf 100644 --- a/app/app.go +++ b/app/app.go @@ -334,9 +334,9 @@ func loadTemplates() (templates map[string]*Template) { templateName := filepath.Base(templateFileName) templateName = strings.TrimSuffix(templateName, filepath.Ext(templateName)) fileNames := make([]string, 0, len(sharedFileNames)+2) - // The base template has to come first, except for the email template, which - // doesn't use it - if templateName != "digest-email" { + // The base template has to come first, except for email ones, which + // don't use it. + if !strings.HasSuffix(templateName, "-email") { fileNames = append(fileNames, "templates/base/page.html") } fileNames = append(fileNames, templateFileName) diff --git a/app/retrogit.go b/app/retrogit.go index d100bcb..4149c90 100644 --- a/app/retrogit.go +++ b/app/retrogit.go @@ -284,6 +284,15 @@ func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) { emailAddress, err := account.GetDigestEmailAddress(githubClient) if err != nil { + if gitHubError, ok := (err).(*github.ErrorResponse); ok { + gitHubStatus := gitHubError.Response.StatusCode + if gitHubStatus == http.StatusUnauthorized || + gitHubStatus == http.StatusForbidden { + c.Errorf(" GitHub auth error while getting email adddress, skipping: %s", err.Error()) + return false, nil + } + } + return false, err } if emailAddress == "disabled" { @@ -292,6 +301,26 @@ func sendDigestForAccount(account *Account, c appengine.Context) (bool, error) { digest, err := newDigest(c, githubClient, account) if err != nil { + if gitHubError, ok := (err).(*github.ErrorResponse); ok { + gitHubStatus := gitHubError.Response.StatusCode + if gitHubStatus == http.StatusUnauthorized || + gitHubStatus == http.StatusForbidden { + c.Errorf(" GitHub auth error while getting digest, sending error email: %s", err.Error()) + var authErrorHtml bytes.Buffer + if err := templates["github-auth-error-email"].Execute(&authErrorHtml, nil); err != nil { + return false, err + } + + digestMessage := &mail.Message{ + Sender: "RetroGit ", + To: []string{emailAddress}, + Subject: "RetroGit Digest Error", + HTMLBody: authErrorHtml.String(), + } + err = mail.Send(c, digestMessage) + return false, err + } + } return false, err } if digest.Empty() { diff --git a/app/templates/digest-email.html b/app/templates/digest-email.html index 9ddbc57..27b2fe9 100644 --- a/app/templates/digest-email.html +++ b/app/templates/digest-email.html @@ -1,22 +1,3 @@ {{template "digest" .Digest}} -
- -
- -

- You are receiving this email because you set up a - RetroGit account. -

- -

- Update your email preferences - | View digest in browser -

- -

- RetroGit is a project by - Mihai Parparita. -

- -
+{{template "email-footer"}} diff --git a/app/templates/github-auth-error-email.html b/app/templates/github-auth-error-email.html new file mode 100644 index 0000000..231edc3 --- /dev/null +++ b/app/templates/github-auth-error-email.html @@ -0,0 +1,16 @@ +

+ A RetroGit digest could not be generated for your account due to a GitHub + authentication error. You may have revoked RetroGit's access (you can see this + on your GitHub settings + page). If you wish to grant it access again, use the button below: +

+ +
+ + +
+ +{{template "email-footer"}} diff --git a/app/templates/shared/email-footer.html b/app/templates/shared/email-footer.html new file mode 100644 index 0000000..96e71b6 --- /dev/null +++ b/app/templates/shared/email-footer.html @@ -0,0 +1,24 @@ +{{define "email-footer"}} + +
+ +
+ +

+ You are receiving this email because you set up a + RetroGit account. +

+ +

+ Update your email preferences + | View digest in browser +

+ +

+ RetroGit is a project by + Mihai Parparita. +

+ +
+ +{{end}}