diff options
| author | Michael Vogt <mvo@ubuntu.com> | 2023-07-26 11:19:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-26 11:19:13 +0200 |
| commit | c0e8ad56f3fab9846615caff4e8f4ac403ca1b9c (patch) | |
| tree | d5450e2bd95cbc2f57798f0df8c009f789d2fc24 | |
| parent | cc5e5e2470b501fc0a486e2deca48c8c49764ce5 (diff) | |
client: fix specificErr handling in SnapshotExport() (#13001)
* client: fix specificErr handling in SnapshotExport() The code was checking generating a `specificErr` but then checked `if err != nil {` (and not check for specificErr) and also lacked a unit test. When adding the code test it became clear that the other issue there is that the response body was never read so r.err() would always return nil. This commit fixes both issues. * client: fix TestClientExportSnapshotSpecificErr naming Co-authored-by: Miguel Pires <miguelpires94@gmail.com> --------- Co-authored-by: Miguel Pires <miguelpires94@gmail.com>
| -rw-r--r-- | client/snapshot.go | 9 | ||||
| -rw-r--r-- | client/snapshot_test.go | 10 |
2 files changed, 16 insertions, 3 deletions
diff --git a/client/snapshot.go b/client/snapshot.go index 0f78e964fe..1d4f69d51d 100644 --- a/client/snapshot.go +++ b/client/snapshot.go @@ -243,9 +243,12 @@ func (client *Client) SnapshotExport(setID uint64) (stream io.ReadCloser, conten defer rsp.Body.Close() var r response - specificErr := r.err(client, rsp.StatusCode) - if err != nil { - return nil, 0, specificErr + dec := json.NewDecoder(rsp.Body) + if err := dec.Decode(&r); err == nil { + specificErr := r.err(client, rsp.StatusCode) + if specificErr != nil { + return nil, 0, specificErr + } } return nil, 0, fmt.Errorf("unexpected status code: %v", rsp.Status) } diff --git a/client/snapshot_test.go b/client/snapshot_test.go index 07284c9d9b..33218a708e 100644 --- a/client/snapshot_test.go +++ b/client/snapshot_test.go @@ -143,6 +143,16 @@ func (cs *clientSuite) TestClientRestoreSnapshots(c *check.C) { cs.testClientSnapshotAction(c, "restore", cs.cli.RestoreSnapshots) } +func (cs *clientSuite) TestClientExportSnapshotSpecificErr(c *check.C) { + content := `{"type":"error","status-code":400,"result":{"message":"boom","kind":"err-kind","value":"err-value"}}` + cs.contentLength = int64(len(content)) + cs.rsp = content + cs.status = 400 + cs.header = http.Header{"Content-Type": []string{"application/json"}} + _, _, err := cs.cli.SnapshotExport(42) + c.Check(err, check.ErrorMatches, "boom") +} + func (cs *clientSuite) TestClientExportSnapshot(c *check.C) { type tableT struct { content string |
