From 5c20ac3f71bddea1dea5993d17009a0cea19b81e Mon Sep 17 00:00:00 2001 From: Heather Lanigan Date: Wed, 14 Aug 2024 14:44:22 -0400 Subject: [PATCH] fix: add info message when controller does not support backup download Rather than a 404 message, print a nice error message if attempting to download a backup archive from a 4.0 controller, or any other which does not support the action. --- api/client/backups/download.go | 3 ++- cmd/juju/backups/download.go | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/api/client/backups/download.go b/api/client/backups/download.go index 6b31814af93..47e65ee6d06 100644 --- a/api/client/backups/download.go +++ b/api/client/backups/download.go @@ -10,6 +10,7 @@ import ( "github.com/juju/errors" "gopkg.in/httprequest.v1" + apiservererrors "github.com/juju/juju/apiserver/errors" "github.com/juju/juju/rpc/params" ) @@ -36,7 +37,7 @@ func (c *Client) Download(filename string) (io.ReadCloser, error) { &resp, ) if err != nil { - return nil, errors.Trace(err) + return nil, errors.Trace(apiservererrors.RestoreError(err)) } return resp.Body, nil } diff --git a/cmd/juju/backups/download.go b/cmd/juju/backups/download.go index 9833672190a..510eadfd07b 100644 --- a/cmd/juju/backups/download.go +++ b/cmd/juju/backups/download.go @@ -88,9 +88,13 @@ func (c *downloadCommand) Run(ctx *cmd.Context) error { // Download the archive. resultArchive, err := client.Download(c.RemoteFilename) if err != nil { + if errors.Is(err, errors.NotFound) { + ctx.Errorf("Download of backup archive files is not supported by this controller.") + return nil + } return errors.Trace(err) } - defer resultArchive.Close() + defer func() { _ = resultArchive.Close() }() // Prepare the local archive. filename := c.ResolveFilename() @@ -98,7 +102,7 @@ func (c *downloadCommand) Run(ctx *cmd.Context) error { if err != nil { return errors.Annotate(err, "while creating local archive file") } - defer archive.Close() + defer func() { _ = archive.Close() }() // Write out the archive. _, err = io.Copy(archive, resultArchive)