From 22d6eae8ead68f73c1cfceef2b61503b2aad47bb Mon Sep 17 00:00:00 2001 From: Ilya Brin <464157+ilyabrin@users.noreply.github.com> Date: Thu, 8 Oct 2026 00:20:47 +0300 Subject: [PATCH] fix: treat 204 and 202 from a delete as success The API answers 204 once a resource is deleted, and 202 when a large folder is deleted in the background, but DeleteResource accepted only 200. Every successful delete came back as "request failed with status 204", in yad as an error message after the file was already gone. Batch deletes go through the same method and are fixed too. --- delete_status_test.go | 36 ++++++++++++++++++++++++++++++++++++ resources.go | 5 +++-- 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 delete_status_test.go diff --git a/delete_status_test.go b/delete_status_test.go new file mode 100644 index 0000000..6339d82 --- /dev/null +++ b/delete_status_test.go @@ -0,0 +1,36 @@ +package disk + +import ( + "context" + "net/http" + "testing" +) + +// The real API answers a delete with 204, or 202 for a large folder deleted +// in the background. Both mean success. +func TestDeleteResourceAcceptsTheRealStatusCodes(t *testing.T) { + for _, status := range []int{http.StatusNoContent, http.StatusAccepted, http.StatusOK} { + client := mockedHttpClient(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete { + t.Errorf("method = %s, want DELETE", r.Method) + } + w.WriteHeader(status) + if status == http.StatusAccepted { + _, _ = w.Write([]byte(`{"href": "https://cloud-api.yandex.net/v1/disk/operations/abc", "method": "GET"}`)) + } + }) + if err := client.DeleteResource(context.Background(), "disk:/old.txt", false); err != nil { + t.Errorf("status %d: %v", status, err) + } + } +} + +func TestDeleteResourceReportsAMissingResource(t *testing.T) { + client := mockedHttpClient(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"error": "DiskNotFoundError", "description": "Resource not found."}`)) + }) + if err := client.DeleteResource(context.Background(), "disk:/missing.txt", false); err == nil { + t.Error("a 404 must be reported as an error") + } +} diff --git a/resources.go b/resources.go index d2dbf20..5bb8c89 100644 --- a/resources.go +++ b/resources.go @@ -65,8 +65,9 @@ func (c *Client) DeleteResource(ctx context.Context, path string, permanently bo } defer resp.Body.Close() - // Use centralized response handling - if _, err := c.handleResponse(resp, []int{200}); err != nil { + // The API answers 204 once the resource is gone, and 202 when a large + // folder is being deleted in the background. 200 is kept for proxies. + if _, err := c.handleResponse(resp, []int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}); err != nil { return fmt.Errorf("delete request failed: %w", err) }