diff --git a/internal/app/worker/wmail/err.go b/internal/app/worker/wmail/err.go index c82525f42..5a40393eb 100644 --- a/internal/app/worker/wmail/err.go +++ b/internal/app/worker/wmail/err.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "github.com/warmbly/warmbly/internal/client/msgraph" "github.com/warmbly/warmbly/internal/errx" "github.com/warmbly/warmbly/internal/infrastructure/eventbus" "github.com/warmbly/warmbly/internal/models" @@ -16,7 +17,7 @@ import ( // itself, and one per mailbox for a bus that closed because we are shutting // down. The relay below still runs either way, so nothing stops acting on them. func reportable(err error) bool { - if errors.Is(err, eventbus.ErrBusClosed) || errors.Is(err, context.Canceled) { + if errors.Is(err, eventbus.ErrBusClosed) || errors.Is(err, context.Canceled) || errors.Is(err, msgraph.ErrUnreadableResponse) { return false } var mailErr *errx.MailError diff --git a/internal/app/worker/wmail/err_test.go b/internal/app/worker/wmail/err_test.go index 4af91c03f..69bc394ab 100644 --- a/internal/app/worker/wmail/err_test.go +++ b/internal/app/worker/wmail/err_test.go @@ -1,9 +1,12 @@ package wmail import ( + "errors" + "fmt" "testing" "time" + "github.com/warmbly/warmbly/internal/client/msgraph" "github.com/warmbly/warmbly/internal/errx" "github.com/warmbly/warmbly/internal/models" ) @@ -42,6 +45,14 @@ func TestTransientProviderFailuresAreNotReportedAsApplicationErrors(t *testing.T } } +// A Graph success body that is not JSON retries on the next pass and is logged +// by the client; it is not an application error. +func TestUnreadableGraphResponseIsNotReported(t *testing.T) { + if reportable(fmt.Errorf("%w: %w", msgraph.ErrUnreadableResponse, errors.New("invalid character"))) { + t.Error("an unreadable Graph response was reportable") + } +} + func TestOnlyWarmblyRateLimitDeactivatesMailbox(t *testing.T) { tests := []struct { name string diff --git a/internal/client/msgraph/error_test.go b/internal/client/msgraph/error_test.go index 74be993b6..198c7d1eb 100644 --- a/internal/client/msgraph/error_test.go +++ b/internal/client/msgraph/error_test.go @@ -120,22 +120,18 @@ func TestFetchMessageStillTreatsNotFoundAsASkip(t *testing.T) { } } -// A success status with a body that is not JSON is the provider failing this -// pass, so it retries like a 5xx instead of surfacing as an unclassified error. -func TestUnreadableSuccessBodyRetriesAsUnreachable(t *testing.T) { +// A success status with a body that is not JSON is named as such, not passed +// up as a bare decode error or dressed as an unreachable server. +func TestUnreadableSuccessBodyIsNamed(t *testing.T) { c := deltaTestClient(t, func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte("\xe4\x9a\x01not json")) }, nil) - unreachable := func(err error) bool { - var mailErr *errx.MailError - return errors.As(err, &mailErr) && mailErr.Code == errx.MailErrorCodeServerUnreachable - } var out listPage - if err := c.doJSON(t.Context(), "GET", graphBase+"/me/mailFolders/inbox/messages", nil, &out); !unreachable(err) { - t.Fatalf("list: got %T %v, want a server-unreachable mail error", err, err) + if err := c.doJSON(t.Context(), "GET", graphBase+"/me/mailFolders/inbox/messages", nil, &out); !errors.Is(err, ErrUnreadableResponse) { + t.Fatalf("list: got %T %v", err, err) } - if _, err := c.FetchMessage(t.Context(), "inbox", "m1"); !unreachable(err) { - t.Fatalf("fetch: got %T %v, want a server-unreachable mail error", err, err) + if _, err := c.FetchMessage(t.Context(), "inbox", "m1"); !errors.Is(err, ErrUnreadableResponse) { + t.Fatalf("fetch: got %T %v", err, err) } } diff --git a/internal/client/msgraph/msgraph.go b/internal/client/msgraph/msgraph.go index 9a564d08e..6c25fc057 100644 --- a/internal/client/msgraph/msgraph.go +++ b/internal/client/msgraph/msgraph.go @@ -12,12 +12,12 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "net" "net/http" "net/url" "sync" - "time" "github.com/rs/zerolog/log" "github.com/warmbly/warmbly/internal/errx" @@ -188,18 +188,19 @@ func (c *Client) doJSON(ctx context.Context, method, url string, in, out any) er var errGraphTrailingJSON = errors.New("graph: response contains extra JSON data") -// unreadableResponse is a success status whose body is not the JSON Graph -// promised: the provider's fault for this pass, retried like a 5xx. The body is -// never logged, because it can carry mail content. -func unreadableResponse(resp *http.Response, err error) *errx.MailError { +// ErrUnreadableResponse is a success status whose body is not the JSON Graph +// promised. It says nothing about the mailbox or reachability, so the pass +// retries without reporting either. +var ErrUnreadableResponse = errors.New("graph: success response could not be decoded") + +// unreadableResponse logs the response's shape, never its body, which can carry mail content. +func unreadableResponse(resp *http.Response, err error) error { log.Warn().Err(err). Int("status", resp.StatusCode). Str("content_type", resp.Header.Get("Content-Type")). Str("content_encoding", resp.Header.Get("Content-Encoding")). Msg("graph: success response could not be decoded") - now := time.Now().UTC() - return errx.WithSendFailure(errx.ErrMailServerUnreachable, errx.SendFailure{Provider: "microsoft", Protocol: "http", - Status: resp.StatusCode, Cause: "unreadable_response", Scope: "mailbox", ObservedAt: now, Disposition: errx.SendRetry}) + return fmt.Errorf("%w: %w", ErrUnreadableResponse, err) } // transportError classifies a failure of the HTTP call itself. The client's