Skip to content

Commit f851639

Browse files
qiaodevcopybara-github
authored andcommitted
fix: fix MIME type error in UploadFromPath and add unit tests. fixes: #247
PiperOrigin-RevId: 748331799
1 parent a44a6c7 commit f851639

File tree

4 files changed

+579
-15
lines changed

4 files changed

+579
-15
lines changed

api_client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ func newAPIError(resp *http.Response) error {
270270

271271
if len(body) > 0 {
272272
if err := json.Unmarshal(body, respWithError); err != nil {
273-
return fmt.Errorf("newAPIError: unmarshal response to error failed: %w. Response: %v", err, string(body))
273+
// Handle plain text error message. File upload backend doesn't return json error message.
274+
return APIError{Code: resp.StatusCode, Status: resp.Status, Message: string(body)}
274275
}
275276
return *respWithError.ErrorInfo
276277
}
@@ -380,7 +381,8 @@ func (ac *apiClient) uploadFile(ctx context.Context, r io.Reader, uploadURL stri
380381
offset += int64(bytesRead)
381382

382383
uploadStatus := resp.Header.Get("X-Goog-Upload-Status")
383-
if uploadStatus != "final" && strings.Contains(uploadStatus, "finalize") {
384+
385+
if uploadStatus != "final" && strings.Contains(uploadCommand, "finalize") {
384386
return nil, fmt.Errorf("send finalize command but doesn't receive final status. Offset %d, Bytes read: %d, Upload status: %s", offset, bytesRead, uploadStatus)
385387
}
386388
if uploadStatus != "active" {

api_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func TestSendStreamRequest(t *testing.T) {
321321
mockResponse: `invalid json`,
322322
mockStatusCode: http.StatusBadRequest,
323323
wantErr: true,
324-
wantErrorMessage: "newAPIError: unmarshal response to error failed: invalid character 'i' looking for beginning of value. Response: invalid json",
324+
wantErrorMessage: "Error 400, Message: invalid json, Status: 400 Bad Request, Details: []",
325325
},
326326
{
327327
name: "Error Response with server error",
@@ -351,7 +351,7 @@ func TestSendStreamRequest(t *testing.T) {
351351
mockResponse: `invalid json`,
352352
mockStatusCode: http.StatusInternalServerError,
353353
wantErr: true,
354-
wantErrorMessage: "newAPIError: unmarshal response to error failed: invalid character 'i' looking for beginning of value. Response: invalid json",
354+
wantErrorMessage: "Error 500, Message: invalid json, Status: 500 Internal Server Error, Details: []",
355355
},
356356
{
357357
name: "Request Error",

files.go

Lines changed: 7 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)