mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-17 16:19:13 +00:00
feat: name an undecodable Graph success body as msgraph.ErrUnreadableResponse and keep it out of worker error reports, so the pass retries without fabricating a server-unreachable event
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user