Skip to content

Commit

Permalink
invalid messageはretryしても完了しないので諦める refs #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed Dec 31, 2020
1 parent 635d17c commit d191fb2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
54 changes: 54 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"

"golang.org/x/xerrors"
)

// ErrInvalidMessage is Messageが処理できない時に返す
var ErrInvalidMessage = &Error{
Code: "InvalidMessage",
Message: "InvalidMessage",
KV: map[string]interface{}{},
}

// Error is Error情報を保持する struct
type Error struct {
Code string
Message string
KV map[string]interface{}
err error
}

// Error is error interface func
func (e *Error) Error() string {
if e.KV == nil || len(e.KV) < 1 {
return fmt.Sprintf("%s: %s: %s", e.Code, e.Message, e.err)
}
return fmt.Sprintf("%s: %s: attribute:%+v :%s", e.Code, e.Message, e.KV, e.err)
}

// Is is err equal check
func (e *Error) Is(target error) bool {
var appErr *Error
if !xerrors.As(target, &appErr) {
return false
}
return e.Code == appErr.Code
}

// Unwrap is return unwrap error
func (e *Error) Unwrap() error {
return e.err
}

// newErrInvalidMessage is return ErrInvalidMessage
func newErrInvalidMessage(message string, kv map[string]interface{}, err error) *Error {
return &Error{
Code: ErrInvalidMessage.Code,
Message: message,
KV: kv,
err: err,
}
}
8 changes: 7 additions & 1 deletion gmail_notify_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
"log"
"net/http"

"golang.org/x/xerrors"
)

// PubSubMessage is the payload of a Pub/Sub event.
Expand Down Expand Up @@ -48,7 +50,11 @@ func GmailNotifyPubSubHandler(w http.ResponseWriter, r *http.Request) {
}

info, err := gmailService.GetErrorReportingInfo(ctx, d.EmailAddress, d.HistoryID, tbfErrorReportingLabelID)
if err != nil {
if xerrors.Is(err, ErrInvalidMessage) {
log.Printf("invalid gmail history: %+v, %v\n", d, err)
w.WriteHeader(http.StatusOK) // Retryしても完了しないので、諦めて終わる
return
} else if err != nil {
log.Printf("gmailService.GetErrorReportingInfo: %v\n", err)
http.Error(w, "InternalServerError", http.StatusInternalServerError)
return
Expand Down
6 changes: 3 additions & 3 deletions gmail_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func (s *GmailService) GetMessage(ctx context.Context, userID string, startHisto
}

if len(res.History) < 1 {
return nil, fmt.Errorf("invalid History List. %+v", res)
return nil, newErrInvalidMessage("invalid gmail.history.list", map[string]interface{}{"history": res}, nil)
}
if len(res.History[0].Messages) < 1 {
return nil, fmt.Errorf("invalid History Messages. %+v", res.History[0])
return nil, newErrInvalidMessage("invalid gmail.history.message", map[string]interface{}{"history.message": res.History[0]}, nil)
}

msg, err := s.gus.Messages.Get(userID, res.History[0].Messages[0].Id).Context(ctx).Do()
Expand All @@ -75,7 +75,7 @@ func (s *GmailService) GetErrorReportingInfo(ctx context.Context, userID string,
}

if len(msg.Payload.Parts) < 2 {
return nil, fmt.Errorf("invalid error reporting mail format.%+v", msg.Payload)
return nil, newErrInvalidMessage("invalid error reporting mail format", map[string]interface{}{"payload": msg.Payload}, nil)
}

var plainText []byte
Expand Down

0 comments on commit d191fb2

Please sign in to comment.