Skip to content

Commit b272ff5

Browse files
committed
chore: address comments, error messages
1 parent 85018b9 commit b272ff5

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

pkg/files/file_stream.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func SendChunkedFile(
2727
chunkSize := int(header.Header.GetChunkSize())
2828
total := int(header.Header.GetFileMeta().GetSize())
2929
if chunkSize == 0 || chunkCount == 0 || total == 0 {
30-
return fmt.Errorf("zero in header: %+v", header.Header)
30+
return fmt.Errorf("file size in header is zero: %+v", header.Header)
3131
}
3232
if err := dst.Send(&v1.FileDataChunk{
3333
Meta: meta,
3434
Chunk: &header,
3535
}); err != nil {
36-
return fmt.Errorf("%w: send error (header)", err)
36+
return fmt.Errorf("unable to send header chunk: %w", err)
3737
}
3838
// allocate the buffer we need for reading from io.Reader
3939
// this is set to the size of the chunks we need to send.
@@ -48,7 +48,7 @@ func SendChunkedFile(
4848
total -= n
4949
if err != nil && total != 0 {
5050
// partial read
51-
return fmt.Errorf("%w: failed read", err)
51+
return fmt.Errorf("unable to read chunk id %d: %w", i, err)
5252
}
5353
if err = dst.Send(&v1.FileDataChunk{
5454
Meta: meta,
@@ -59,7 +59,7 @@ func SendChunkedFile(
5959
},
6060
},
6161
}); err != nil {
62-
return fmt.Errorf("%w: send error (content)", err)
62+
return fmt.Errorf("unable to send chunk id %d: %w", i, err)
6363
}
6464
}
6565

@@ -75,7 +75,7 @@ func RecvChunkedFile(
7575
// receive the header first
7676
chunk, err := src.Recv()
7777
if err != nil {
78-
return header, fmt.Errorf("%w: header error", err)
78+
return header, fmt.Errorf("unable to receive header chunk: %w", err)
7979
}
8080

8181
// validate and extract header info
@@ -90,7 +90,7 @@ func RecvChunkedFile(
9090
total := int(header.GetSize())
9191

9292
if chunkSize == 0 || chunkCount == 0 || total == 0 {
93-
return header, fmt.Errorf("zero in header: %v", headerChunk)
93+
return header, fmt.Errorf("file size in header is zero: %+v", headerChunk)
9494
}
9595

9696
return header, recvContents(src, dst, chunkCount, chunkSize, total)
@@ -107,15 +107,15 @@ func recvContents(
107107
for i := 0; i < chunkCount; i++ {
108108
chunk, err := src.Recv()
109109
if err != nil {
110-
return fmt.Errorf("%w: failed to read content", err)
110+
return fmt.Errorf("unable to receive chunk id %d: %w", i, err)
111111
}
112112

113113
if err = validateRecvChunk(chunk, chunkSize, chunkCount-1, i); err != nil {
114114
return err
115115
}
116116
data := chunk.GetContent().GetData()
117117
if _, err = dst.Write(data); err != nil {
118-
return fmt.Errorf("%w: failed write", err)
118+
return fmt.Errorf("unable to write chunk id %d: %w", i, err)
119119
}
120120
totalSize -= len(data)
121121
if 0 > totalSize {
@@ -129,17 +129,17 @@ func recvContents(
129129
return nil
130130
}
131131

132-
func validateRecvChunk(chunk *v1.FileDataChunk, chunkSize, lastChunkIndex, i int) error {
132+
func validateRecvChunk(chunk *v1.FileDataChunk, chunkSize, lastChunkIndex, chunkID int) error {
133133
content := chunk.GetContent()
134134
if content == nil {
135-
return fmt.Errorf("no content in chunk id %d", i)
135+
return fmt.Errorf("no content in chunk id %d", chunkID)
136136
}
137-
if content.GetChunkId() != uint32(i) {
137+
if content.GetChunkId() != uint32(chunkID) {
138138
return fmt.Errorf("content chunk id of %d does not match expected id of %d",
139-
content.GetChunkId(), i)
139+
content.GetChunkId(), chunkID)
140140
}
141141
data := content.GetData()
142-
if len(data) != chunkSize && i != lastChunkIndex {
142+
if len(data) != chunkSize && chunkID != lastChunkIndex {
143143
return fmt.Errorf("content chunk size of %d does not match expected size of %d",
144144
len(data), chunkSize)
145145
}

pkg/files/file_stream_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestSendChunkedFile(t *testing.T) {
4343
}{
4444
{
4545
name: "Test 1: zero header",
46-
expectedErrString: "zero in header",
46+
expectedErrString: "file size in header is zero",
4747
},
4848
{
4949
name: "Test 2: under chunk size",
@@ -96,7 +96,7 @@ func TestSendChunkedFile(t *testing.T) {
9696
},
9797
},
9898
content: randBytes(2300),
99-
expectedErrString: "failed read",
99+
expectedErrString: "unable to read chunk id 1",
100100
},
101101
{
102102
name: "Test 6: read over size (meta file size > actual content size)",
@@ -128,7 +128,7 @@ func TestSendChunkedFile(t *testing.T) {
128128
clientFunc: func(cl *v1fakes.FakeFileService_GetFileStreamServer) {
129129
cl.SendReturns(fmt.Errorf("error"))
130130
},
131-
expectedErrString: "send error (header)",
131+
expectedErrString: "unable to send header chunk",
132132
},
133133
{
134134
name: "Test 8: send error (content)",
@@ -152,7 +152,7 @@ func TestSendChunkedFile(t *testing.T) {
152152
return nil
153153
})
154154
},
155-
expectedErrString: "send error (content)",
155+
expectedErrString: "unable to send chunk id 0",
156156
},
157157
}
158158

@@ -248,7 +248,7 @@ func TestRecvChunkedFile(t *testing.T) {
248248
},
249249
},
250250
},
251-
expectedErrString: "zero in header:",
251+
expectedErrString: "file size in header is zero",
252252
},
253253
{
254254
name: "Test 3: data unmatched",
@@ -404,7 +404,7 @@ func TestRecvChunkedFile(t *testing.T) {
404404
err: recvErr,
405405
},
406406
},
407-
expectedErrString: "recv error: failed to read content",
407+
expectedErrString: "unable to receive chunk id 0",
408408
},
409409
{
410410
name: "Test 8: header recv error",
@@ -413,7 +413,7 @@ func TestRecvChunkedFile(t *testing.T) {
413413
err: recvErr,
414414
},
415415
},
416-
expectedErrString: "recv error: header error",
416+
expectedErrString: "unable to receive header chunk",
417417
},
418418
{
419419
name: "Test 9: data unmatched - nil content",
@@ -467,7 +467,7 @@ func TestRecvChunkedFile(t *testing.T) {
467467
},
468468
},
469469
writer: &badWriter{},
470-
expectedErrString: "error: failed write",
470+
expectedErrString: "unable to write chunk id 0",
471471
},
472472
{
473473
name: "Test 11: no error",

0 commit comments

Comments
 (0)