Skip to content

Commit

Permalink
chore: refactor - rm write/Error, exec goimports, simplify return fro…
Browse files Browse the repository at this point in the history
…m Service.HandleWrite
  • Loading branch information
karel-rehor committed Aug 9, 2024
1 parent 580d1ea commit 584c614
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 145 deletions.
3 changes: 2 additions & 1 deletion api/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/influxdata/influxdb-client-go/v2/api"
apiHttp "github.com/influxdata/influxdb-client-go/v2/api/http"
"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/influxdata/influxdb-client-go/v2/domain"
influxdb2 "github.com/influxdata/influxdb-client-go/v2/internal/examples"
Expand Down Expand Up @@ -123,7 +124,7 @@ func ExampleWriteAPI_errors() {
go func() {
for err := range errorsCh {
fmt.Printf("write error: %s\n", err.Error())
fmt.Printf("trace-id: %s\n", err.(*write.Error).GetHeader("Trace-ID"))
fmt.Printf("trace-id: %s\n", err.(*apiHttp.Error).Header.Get("Trace-ID"))
}
}()
// write some points
Expand Down
64 changes: 0 additions & 64 deletions api/write/error.go

This file was deleted.

66 changes: 0 additions & 66 deletions api/write/error_test.go

This file was deleted.

16 changes: 7 additions & 9 deletions api/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,13 @@ func TestWriteApiErrorHeaders(t *testing.T) {
for i := 0; i < 3; i++ {
recErr = <-errCh
assert.NotNil(t, recErr, "errCh should not run out of values")
header, okh := recErr.(*write.Error).HTTPHeader()
assert.Nil(t, okh)
assert.Len(t, header, 6)
assert.NotEqual(t, "", recErr.(*write.Error).GetHeader("Date"))
assert.NotEqual(t, "", recErr.(*write.Error).GetHeader("Content-Length"))
assert.NotEqual(t, "", recErr.(*write.Error).GetHeader("Content-Type"))
assert.Equal(t, strconv.Itoa(i+1), recErr.(*write.Error).GetHeader("X-Call-Count"))
assert.Equal(t, "Not All Correct", recErr.(*write.Error).GetHeader("X-Test-Val1"))
assert.Equal(t, "Atlas LV-3B", recErr.(*write.Error).GetHeader("X-Test-Val2"))
assert.Len(t, recErr.(*http.Error).Header, 6)
assert.NotEqual(t, "", recErr.(*http.Error).Header.Get("Date"))
assert.NotEqual(t, "", recErr.(*http.Error).Header.Get("Content-Length"))
assert.NotEqual(t, "", recErr.(*http.Error).Header.Get("Content-Type"))
assert.Equal(t, strconv.Itoa(i+1), recErr.(*http.Error).Header.Get("X-Call-Count"))
assert.Equal(t, "Not All Correct", recErr.(*http.Error).Header.Get("X-Test-Val1"))
assert.Equal(t, "Atlas LV-3B", recErr.(*http.Error).Header.Get("X-Test-Val2"))
}
wg.Done()
}()
Expand Down
5 changes: 4 additions & 1 deletion internal/write/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ func (w *Service) HandleWrite(ctx context.Context, batch *Batch) error {
}
log.Error(logMessage)
}
return write.NewError(perror, fmt.Sprintf("write failed (retry attempts %d)", batchToWrite.RetryAttempts))
log.Errorf("Write failed (retry attempts %d): Status Code %d",
batchToWrite.RetryAttempts,
perror.StatusCode)
return perror
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/write/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestMaxRetryTime(t *testing.T) {
err = srv.HandleWrite(ctx, b)
require.NotNil(t, err)
// 1st Batch expires and writing 2nd trows error
assert.Equal(t, "write failed (retry attempts 1):\n Unexpected status code 429", err.Error())
assert.Equal(t, "Unexpected status code 429", err.Error())
assert.Equal(t, 1, srv.retryQueue.list.Len())

//wait until remaining accumulated retryDelay has passed, because there hasn't been a successful write yet
Expand Down Expand Up @@ -715,7 +715,7 @@ func TestHttpErrorHeaders(t *testing.T) {
write.DefaultOptions())
err := svc.HandleWrite(context.Background(), NewBatch("1", 20))
assert.Error(t, err)
assert.Equal(t, "write failed (retry attempts 0):\n 400 Bad Request: { \"code\": \"bad request\", \"message\": \"test header\" }", err.Error())
assert.Equal(t, "Not All Correct", err.(*write.Error).GetHeader("X-Test-Val1"))
assert.Equal(t, "Atlas LV-3B", err.(*write.Error).GetHeader("X-Test-Val2"))
assert.Equal(t, "400 Bad Request: { \"code\": \"bad request\", \"message\": \"test header\" }", err.Error())
assert.Equal(t, "Not All Correct", err.(*http.Error).Header.Get("X-Test-Val1"))
assert.Equal(t, "Atlas LV-3B", err.(*http.Error).Header.Get("X-Test-Val2"))
}

0 comments on commit 584c614

Please sign in to comment.