Skip to content

Commit

Permalink
index out of rangeになったので、Validationとログを強化した refs #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed Dec 31, 2020
1 parent 96d6b8d commit 635d17c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions gmail_notify_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ func GmailNotifyPubSubHandler(w http.ResponseWriter, r *http.Request) {
var m PubSubMessage
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("ioutil.ReadAll: %v", err)
log.Printf("ioutil.ReadAll: %v\n", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if err := json.Unmarshal(body, &m); err != nil {
log.Printf("json.Unmarshal: %v", err)
log.Printf("json.Unmarshal: %v\n", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}

var d NotifyData
if err := json.Unmarshal(m.Message.Data, &d); err != nil {
log.Printf("json.Unmarshal: %v", err)
log.Printf("json.Unmarshal: %v\n", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}

info, err := gmailService.GetErrorReportingInfo(ctx, d.EmailAddress, d.HistoryID, tbfErrorReportingLabelID)
if err != nil {
log.Printf("gmailService.GetErrorReportingInfo: %v", err)
log.Printf("gmailService.GetErrorReportingInfo: %v\n", err)
http.Error(w, "InternalServerError", http.StatusInternalServerError)
return
}
Expand Down
9 changes: 8 additions & 1 deletion gmail_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (s *GmailService) GetMessage(ctx context.Context, userID string, startHisto
return nil, fmt.Errorf("failed GmailUserService.Watch.userID=%s,req=%#v : %w", userID, err)
}

if len(res.History) < 1 {
return nil, fmt.Errorf("invalid History List. %+v", res)
}
if len(res.History[0].Messages) < 1 {
return nil, fmt.Errorf("invalid History Messages. %+v", res.History[0])
}

msg, err := s.gus.Messages.Get(userID, res.History[0].Messages[0].Id).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed GmailUserService.Message.Get.userID=%s,req=%#v : %w", userID, err)
Expand All @@ -68,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")
return nil, fmt.Errorf("invalid error reporting mail format.%+v", msg.Payload)
}

var plainText []byte
Expand Down

0 comments on commit 635d17c

Please sign in to comment.