diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..f343fa9 --- /dev/null +++ b/errors.go @@ -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, + } +} diff --git a/gmail_notify_handler.go b/gmail_notify_handler.go index d1ab4bb..95637a6 100644 --- a/gmail_notify_handler.go +++ b/gmail_notify_handler.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "log" "net/http" + + "golang.org/x/xerrors" ) // PubSubMessage is the payload of a Pub/Sub event. @@ -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 diff --git a/gmail_service.go b/gmail_service.go index a12323c..9fabd42 100644 --- a/gmail_service.go +++ b/gmail_service.go @@ -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() @@ -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