From 1b7a8fdcc52166fd3033d5ed85e902d6a775d9ab Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 13 Jun 2023 12:50:18 +0200 Subject: Report details of feed transport error --- client/go/internal/cli/cmd/document.go | 3 +++ client/go/internal/cli/cmd/document_test.go | 18 ++++++++++++++++++ client/go/internal/cli/cmd/feed_test.go | 9 +++++++++ client/go/internal/mock/http.go | 12 ++++++++++++ client/go/internal/vespa/document/dispatcher.go | 11 +++++++++-- 5 files changed, 51 insertions(+), 2 deletions(-) (limited to 'client') diff --git a/client/go/internal/cli/cmd/document.go b/client/go/internal/cli/cmd/document.go index 07a98d2e626..0ed68e30ced 100644 --- a/client/go/internal/cli/cmd/document.go +++ b/client/go/internal/cli/cmd/document.go @@ -136,6 +136,9 @@ func readDocument(id string, timeoutSecs int, printCurl bool, cli *CLI) error { } func operationResult(read bool, doc document.Document, service *vespa.Service, result document.Result) util.OperationResult { + if result.Err != nil { + return util.Failure(result.Err.Error()) + } bodyReader := bytes.NewReader(result.Body) if result.HTTPStatus == 200 { if read { diff --git a/client/go/internal/cli/cmd/document_test.go b/client/go/internal/cli/cmd/document_test.go index 00f98ee1333..6e671190959 100644 --- a/client/go/internal/cli/cmd/document_test.go +++ b/client/go/internal/cli/cmd/document_test.go @@ -6,6 +6,7 @@ package cmd import ( "encoding/json" + "fmt" "os" "strconv" "testing" @@ -100,6 +101,10 @@ func TestDocumentPutServerError(t *testing.T) { assertDocumentServerError(t, 501, "Server error") } +func TestDocumentPutTransportError(t *testing.T) { + assertDocumentTransportError(t, "Transport error") +} + func TestDocumentGet(t *testing.T) { assertDocumentGet([]string{"document", "get", "id:mynamespace:music::a-head-full-of-dreams"}, "id:mynamespace:music::a-head-full-of-dreams", t) @@ -172,6 +177,19 @@ func assertDocumentGet(arguments []string, documentId string, t *testing.T) { assert.Equal(t, "GET", client.LastRequest.Method) } +func assertDocumentTransportError(t *testing.T, errorMessage string) { + client := &mock.HTTPClient{} + client.NextResponseError(fmt.Errorf(errorMessage)) + cli, _, stderr := newTestCLI(t) + cli.httpClient = client + assert.NotNil(t, cli.Run("document", "put", + "id:mynamespace:music::a-head-full-of-dreams", + "testdata/A-Head-Full-of-Dreams-Put.json")) + assert.Equal(t, + "Error: "+errorMessage+"\n", + stderr.String()) +} + func assertDocumentError(t *testing.T, status int, errorMessage string) { client := &mock.HTTPClient{} client.NextResponseString(status, errorMessage) diff --git a/client/go/internal/cli/cmd/feed_test.go b/client/go/internal/cli/cmd/feed_test.go index bd0b9544e37..96df24f2035 100644 --- a/client/go/internal/cli/cmd/feed_test.go +++ b/client/go/internal/cli/cmd/feed_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "fmt" "os" "path/filepath" "testing" @@ -78,4 +79,12 @@ func TestFeed(t *testing.T) { httpClient.NextResponseString(200, `{"message":"OK"}`) require.Nil(t, cli.Run("feed", "-")) assert.Equal(t, want, stdout.String()) + + httpClient.NextResponseString(500, `{"message":"it's broken yo"}`) + require.Nil(t, cli.Run("feed", jsonFile1)) + assert.Equal(t, "feed: got status 500 ({\"message\":\"it's broken yo\"}) for put id:ns:type::doc1: retrying\n", stderr.String()) + stderr.Reset() + httpClient.NextResponseError(fmt.Errorf("something else is broken")) + require.Nil(t, cli.Run("feed", jsonFile1)) + assert.Equal(t, "feed: got error \"something else is broken\" (no body) for put id:ns:type::doc1: retrying\n", stderr.String()) } diff --git a/client/go/internal/mock/http.go b/client/go/internal/mock/http.go index f176870a940..8bab716ea60 100644 --- a/client/go/internal/mock/http.go +++ b/client/go/internal/mock/http.go @@ -14,6 +14,9 @@ type HTTPClient struct { // The responses to return for future requests. Once a response is consumed, it's removed from this slice. nextResponses []HTTPResponse + // The error to return for the next request. If non-nil, this error is returned before any responses in nextResponses. + nextError error + // LastRequest is the last HTTP request made through this. LastRequest *http.Request @@ -48,7 +51,16 @@ func (c *HTTPClient) NextResponse(response HTTPResponse) { c.nextResponses = append(c.nextResponses, response) } +func (c *HTTPClient) NextResponseError(err error) { + c.nextError = err +} + func (c *HTTPClient) Do(request *http.Request, timeout time.Duration) (*http.Response, error) { + if c.nextError != nil { + err := c.nextError + c.nextError = nil + return nil, err + } response := HTTPResponse{Status: 200} if len(c.nextResponses) > 0 { response = c.nextResponses[0] diff --git a/client/go/internal/vespa/document/dispatcher.go b/client/go/internal/vespa/document/dispatcher.go index d9273d2f677..fa4424809cf 100644 --- a/client/go/internal/vespa/document/dispatcher.go +++ b/client/go/internal/vespa/document/dispatcher.go @@ -71,8 +71,15 @@ func (d *Dispatcher) logResult(doc Document, result Result, retry bool) { return } var msg strings.Builder - msg.WriteString("feed: got status ") - msg.WriteString(strconv.Itoa(result.HTTPStatus)) + msg.WriteString("feed: got ") + if result.Err != nil { + msg.WriteString("error \"") + msg.WriteString(result.Err.Error()) + msg.WriteString("\"") + } else { + msg.WriteString("status ") + msg.WriteString(strconv.Itoa(result.HTTPStatus)) + } msg.WriteString(" (") if result.Body != nil { msg.Write(result.Body) -- cgit v1.2.3