Skip to content

Commit

Permalink
Log (sanitized) request envelope in case of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
petergtz committed Oct 25, 2019
1 parent be5f0f9 commit 568ae33
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
29 changes: 26 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package github

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"runtime/debug"

"github.com/petergtz/go-alexa"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"

Expand Down Expand Up @@ -41,7 +44,7 @@ func NewGithubErrorReporter(owner, repo, token string, logger *zap.SugaredLogger
}
}

func (r *GithubErrorReporter) ReportPanic(e interface{}) {
func (r *GithubErrorReporter) ReportPanic(e interface{}, requestEnv *alexa.RequestEnvelope) {
errorID := rand.Int63()
errorString := errorStringFrom(e)

Expand All @@ -68,7 +71,14 @@ func (r *GithubErrorReporter) ReportPanic(e interface{}) {
_, snsErr := r.snsClient.Publish(&sns.PublishInput{
TopicArn: aws.String(r.snsTopicArn),
Subject: aws.String(fmt.Sprintf(r.repo+": Internal Server Error (ErrID: %v)", errorID)),
Message: aws.String(fmt.Sprintf("ERROR DETAILS:\n\n%s\nCloudWatch Query: %v", stringify(attributes), fmt.Sprintf(r.logsURL, errorID))),
Message: aws.String(fmt.Sprintf(`ERROR DETAILS:
%s
ALEXA REQUEST:
%v
CLOUDWATCH QUERY:
%v`, stringify(attributes), alexaRequestString(requestEnv), fmt.Sprintf(r.logsURL, errorID))),
})

if snsErr != nil {
Expand All @@ -79,7 +89,7 @@ func (r *GithubErrorReporter) ReportPanic(e interface{}) {
}

func (r *GithubErrorReporter) ReportError(e error) {
r.ReportPanic(e)
r.ReportPanic(e, nil)
}

func errorStringFrom(e interface{}) string {
Expand All @@ -104,3 +114,16 @@ func slicify(m map[string]interface{}) []interface{} {
}
return result
}

func alexaRequestString(requestEnv *alexa.RequestEnvelope) string {
if requestEnv == nil {
return "Not available."
}
r := *requestEnv
r.Session.User.AccessToken = "<REDACTED>"
buf, e := json.MarshalIndent(r, "", " ")
if e != nil {
return "Error while marshalling request. Error: " + e.Error()
}
return string(buf)
}
4 changes: 2 additions & 2 deletions skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ErrorInterpreter interface {
}

type ErrorReporter interface {
ReportPanic(e interface{})
ReportPanic(e interface{}, requestEnv *alexa.RequestEnvelope)
ReportError(e error)
}
type JournalSkill struct {
Expand Down Expand Up @@ -105,7 +105,7 @@ type SessionAttributes struct {
func (h *JournalSkill) ProcessRequest(requestEnv *alexa.RequestEnvelope) (responseEnv *alexa.ResponseEnvelope) {
defer func() {
if e := recover(); e != nil {
h.errorReporter.ReportPanic(e)
h.errorReporter.ReportPanic(e, requestEnv)
responseEnv = internalError()
}
}()
Expand Down

0 comments on commit 568ae33

Please sign in to comment.