Skip to content

Commit

Permalink
Improve error message when chunk size > limit
Browse files Browse the repository at this point in the history
Fixes open-policy-agent#5155

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Sep 19, 2022
1 parent 98b692e commit 115681d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion plugins/logs/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func (enc *chunkEncoder) Write(event EventV1) (result [][]byte, err error) {
if len(bs) == 0 {
return nil, nil
} else if int64(len(bs)+2) > enc.limit {
return nil, fmt.Errorf("upload chunk size too small")
return nil, fmt.Errorf("upload chunk size (%d) exceeds upload_size_limit_bytes (%d)",
int64(len(bs)+2), enc.limit)
}

if int64(len(bs)+enc.bytesWritten+1) > enc.softLimit {
Expand Down
30 changes: 30 additions & 0 deletions plugins/logs/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ func TestChunkEncoder(t *testing.T) {
}
}

func TestChunkEncoderSizeLimit(t *testing.T) {
enc := newChunkEncoder(1).WithMetrics(metrics.New())
var result interface{} = false
var expInput interface{} = map[string]interface{}{"method": "GET"}
ts, err := time.Parse(time.RFC3339Nano, "2018-01-01T12:00:00.123456Z")
if err != nil {
t.Fatal(err)
}
event := EventV1{
Labels: map[string]string{
"id": "test-instance-id",
"app": "example-app",
},
DecisionID: "123",
Path: "foo/bar",
Input: &expInput,
Result: &result,
RequestedBy: "test",
Timestamp: ts,
}
_, err = enc.Write(event)
if err == nil {
t.Error("Expected error as upload chunk size exceeds configured limit")
}
expected := "upload chunk size (200) exceeds upload_size_limit_bytes (1)"
if err.Error() != expected {
t.Errorf("expected: '%s', got: '%s'", expected, err.Error())
}
}

func TestChunkEncoderAdaptive(t *testing.T) {

enc := newChunkEncoder(1000).WithMetrics(metrics.New())
Expand Down

0 comments on commit 115681d

Please sign in to comment.