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.
This commit is contained in:
Mihai Parparita 2014-12-20 21:38:09 -08:00
parent 2851ad3ac2
commit 735ac7ffb3
5 changed files with 73 additions and 23 deletions

View file

@ -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)

View file

@ -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 <digests@retrogit.com>",
To: []string{emailAddress},
Subject: "RetroGit Digest Error",
HTMLBody: authErrorHtml.String(),
}
err = mail.Send(c, digestMessage)
return false, err
}
}
return false, err
}
if digest.Empty() {

View file

@ -1,22 +1,3 @@
{{template "digest" .Digest}}
<hr noshade size="1" color="#ccc">
<div style="{{style "proportional" "email-footer"}}">
<p style="{{style "email-footer.paragraph"}}">
You are receiving this email because you set up a
<a href="{{absoluteRouteUrl "index"}}" style="{{style "email-footer.link"}}">RetroGit</a> account.
</p>
<p style="{{style "email-footer.paragraph"}}">
<a href="{{absoluteRouteUrl "settings"}}" style="{{style "email-footer.link"}}">Update your email preferences</a>
| <a href="{{absoluteRouteUrl "view-digest"}}" style="{{style "email-footer.link"}}">View digest in browser</a>
</p>
<p style="{{style "email-footer.paragraph"}}">
RetroGit is a project by
<a href="http://persistent.info" style="{{style "email-footer.link"}}">Mihai Parparita</a>.
</p>
</div>
{{template "email-footer"}}

View file

@ -0,0 +1,16 @@
<p>
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 <a href="https://github.com/settings/applications">GitHub settings
page</a>). If you wish to grant it access again, use the button below:
</p>
<form id="sign-in-form" method="POST" action="{{absoluteRouteUrl "sign-in"}}">
<input type="submit" class="action-button" value="Sign In with GitHub">
<label>
<input type="checkbox" name="include_private" value="1" checked>
Include private repositories
</label>
</form>
{{template "email-footer"}}

View file

@ -0,0 +1,24 @@
{{define "email-footer"}}
<hr noshade size="1" color="#ccc">
<div style="{{style "proportional" "email-footer"}}">
<p style="{{style "email-footer.paragraph"}}">
You are receiving this email because you set up a
<a href="{{absoluteRouteUrl "index"}}" style="{{style "email-footer.link"}}">RetroGit</a> account.
</p>
<p style="{{style "email-footer.paragraph"}}">
<a href="{{absoluteRouteUrl "settings"}}" style="{{style "email-footer.link"}}">Update your email preferences</a>
| <a href="{{absoluteRouteUrl "view-digest"}}" style="{{style "email-footer.link"}}">View digest in browser</a>
</p>
<p style="{{style "email-footer.paragraph"}}">
RetroGit is a project by
<a href="http://persistent.info" style="{{style "email-footer.link"}}">Mihai Parparita</a>.
</p>
</div>
{{end}}