Browse Source

fix: Handle io.ReadAll errors when reading error response bodies

In fetchFileContent and DownloadFile, the error from io.ReadAll was
ignored when reading the filer's error response body. Now properly
handles these errors to provide complete error messages.
pull/7633/head
chrislu 4 weeks ago
parent
commit
1e7b865f30
  1. 11
      weed/admin/handlers/file_browser_handlers.go

11
weed/admin/handlers/file_browser_handlers.go

@ -510,7 +510,10 @@ func (h *FileBrowserHandlers) fetchFileContent(filePath string, timeout time.Dur
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("filer returned status %d but failed to read response body: %w", resp.StatusCode, err)
}
return "", fmt.Errorf("filer returned status %d: %s", resp.StatusCode, string(body))
}
@ -578,7 +581,11 @@ func (h *FileBrowserHandlers) DownloadFile(c *gin.Context) {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(resp.StatusCode, gin.H{"error": fmt.Sprintf("Filer returned status %d but failed to read response body: %v", resp.StatusCode, err)})
return
}
c.JSON(resp.StatusCode, gin.H{"error": fmt.Sprintf("Filer returned status %d: %s", resp.StatusCode, string(body))})
return
}

Loading…
Cancel
Save