From 4d1f265c6287cc11229dbb548391b7fa685f6129 Mon Sep 17 00:00:00 2001 From: Guillame Tardif Date: Fri, 27 Nov 2020 18:18:14 +0100 Subject: [PATCH] Adding error progress indication when errors Signed-off-by: Guillame Tardif --- aci/aci.go | 1 + aci/volumes.go | 16 ++++++++++------ ecs/down.go | 2 +- ecs/wait.go | 8 ++------ local/compose.go | 11 +++++++---- progress/event.go | 13 +++++++++---- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/aci/aci.go b/aci/aci.go index bc8c19b55..d576d2249 100644 --- a/aci/aci.go +++ b/aci/aci.go @@ -116,6 +116,7 @@ func createOrUpdateACIContainers(ctx context.Context, aciContext store.AciContex groupDefinition, ) if err != nil { + w.Event(progress.ErrorEvent(groupDisplay)) return err } diff --git a/aci/volumes.go b/aci/volumes.go index a09583fd3..2c012b534 100644 --- a/aci/volumes.go +++ b/aci/volumes.go @@ -87,12 +87,14 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int w.Event(progress.NewEvent(opts.Account, progress.Working, "Validating")) accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID) if err != nil { + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, err } account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "") if err == nil { w.Event(progress.NewEvent(opts.Account, progress.Done, "Use existing")) } else if !account.HasHTTPStatus(http.StatusNotFound) { + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, err } else { result, err := accountClient.CheckNameAvailability(ctx, storage.AccountCheckNameAvailabilityParameters{ @@ -100,9 +102,11 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int Type: to.StringPtr("Microsoft.Storage/storageAccounts"), }) if err != nil { + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, err } if !*result.NameAvailable { + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, errors.New("error: " + *result.Message) } parameters := defaultStorageAccountParams(cs.aciContext) @@ -111,16 +115,16 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int future, err := accountClient.Create(ctx, cs.aciContext.ResourceGroup, opts.Account, parameters) if err != nil { - w.Event(progress.ErrorEvent(opts.Account, "Error")) + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, err } if err := future.WaitForCompletionRef(ctx, accountClient.Client); err != nil { - w.Event(progress.ErrorEvent(opts.Account, "Error")) + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, err } account, err = future.Result(accountClient) if err != nil { - w.Event(progress.ErrorEvent(opts.Account, "Error")) + w.Event(progress.ErrorEvent(opts.Account)) return volumes.Volume{}, err } w.Event(progress.CreatedEvent(opts.Account)) @@ -133,16 +137,16 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int fileShare, err := fileShareClient.Get(ctx, cs.aciContext.ResourceGroup, *account.Name, name, "") if err == nil { - w.Event(progress.ErrorEvent(name, "Error")) + w.Event(progress.ErrorEvent(name)) return volumes.Volume{}, errors.Wrapf(errdefs.ErrAlreadyExists, "Azure fileshare %q already exists", name) } if !fileShare.HasHTTPStatus(http.StatusNotFound) { - w.Event(progress.ErrorEvent(name, "Error")) + w.Event(progress.ErrorEvent(name)) return volumes.Volume{}, err } fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, name, storage.FileShare{}) if err != nil { - w.Event(progress.ErrorEvent(name, "Error")) + w.Event(progress.ErrorEvent(name)) return volumes.Volume{}, err } w.Event(progress.CreatedEvent(name)) diff --git a/ecs/down.go b/ecs/down.go index 8d1223249..bd1c434f5 100644 --- a/ecs/down.go +++ b/ecs/down.go @@ -68,7 +68,7 @@ func doDelete(ctx context.Context, delete func(ctx context.Context, arn string) w.Event(progress.RemovingEvent(r.LogicalID)) err := delete(ctx, r.ARN) if err != nil { - w.Event(progress.ErrorEvent(r.LogicalID, "Error")) + w.Event(progress.ErrorEvent(r.LogicalID)) return err } w.Event(progress.RemovedEvent(r.LogicalID)) diff --git a/ecs/wait.go b/ecs/wait.go index af62f902a..183c59546 100644 --- a/ecs/wait.go +++ b/ecs/wait.go @@ -101,11 +101,7 @@ func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, op } } } - w.Event(progress.Event{ - ID: resource, - Status: progressStatus, - StatusText: fmt.Sprintf("%s %s", toCamelCase(status), reason), - }) + w.Event(progress.NewEvent(resource, progressStatus, fmt.Sprintf("%s %s", toCamelCase(status), reason))) } if operation != stackCreate || stackErr != nil { continue @@ -116,7 +112,7 @@ func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, op } stackErr = err operation = stackDelete - w.Event(progress.ErrorEvent(name, err.Error())) + w.Event(progress.ErrorMessageEvent(name, err.Error())) } } diff --git a/local/compose.go b/local/compose.go index 43cc509a3..c550d5080 100644 --- a/local/compose.go +++ b/local/compose.go @@ -120,11 +120,13 @@ func (s *composeService) Down(ctx context.Context, projectName string) error { w.Event(progress.NewEvent(getContainerName(container), progress.Working, "Stopping")) err := s.apiClient.ContainerStop(ctx, container.ID, nil) if err != nil { + w.Event(progress.ErrorMessageEvent(getContainerName(container), "Error while Stopping")) return err } w.Event(progress.RemovingEvent(getContainerName(container))) err = s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{}) if err != nil { + w.Event(progress.ErrorMessageEvent(getContainerName(container), "Error while Removing")) return err } w.Event(progress.RemovedEvent(getContainerName(container))) @@ -583,6 +585,7 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi w := progress.ContextWriter(ctx) w.Event(progress.CreatingEvent(networkEventName)) if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil { + w.Event(progress.ErrorEvent(networkEventName)) return errors.Wrapf(err, "failed to create network %s", n.Name) } w.Event(progress.CreatedEvent(networkEventName)) @@ -599,9 +602,8 @@ func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string w.Event(progress.RemovingEvent(eventName)) if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil { - msg := fmt.Sprintf("failed to create network %s", networkID) - w.Event(progress.ErrorEvent(eventName, "Error: "+msg)) - return errors.Wrapf(err, msg) + w.Event(progress.ErrorEvent(eventName)) + return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID)) } w.Event(progress.RemovedEvent(eventName)) @@ -623,10 +625,11 @@ func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeCo Driver: volume.Driver, DriverOpts: volume.DriverOpts, }) - w.Event(progress.CreatedEvent(eventName)) if err != nil { + w.Event(progress.ErrorEvent(eventName)) return err } + w.Event(progress.CreatedEvent(eventName)) } return err } diff --git a/progress/event.go b/progress/event.go index 739dd21d0..828ca7156 100644 --- a/progress/event.go +++ b/progress/event.go @@ -42,11 +42,16 @@ type Event struct { spinner *spinner } -// ErrorEvent creates a new Error Event -func ErrorEvent(ID string, msg string) Event { +// ErrorMessageEvent creates a new Error Event with message +func ErrorMessageEvent(ID string, msg string) Event { return NewEvent(ID, Error, msg) } +// ErrorEvent creates a new Error Event +func ErrorEvent(ID string) Event { + return NewEvent(ID, Error, "Error") +} + // CreatingEvent creates a new Create in progress Event func CreatingEvent(ID string) Event { return NewEvent(ID, Working, "Creating...") @@ -57,12 +62,12 @@ func CreatedEvent(ID string) Event { return NewEvent(ID, Done, "Created") } -// CreatingEvent creates a new Create in progress Event +// RemovingEvent creates a new Removing in progress Event func RemovingEvent(ID string) Event { return NewEvent(ID, Working, "Removing...") } -// CreatedEvent creates a new Created (done) Event +// RemovedEvent creates a new removed (done) Event func RemovedEvent(ID string) Event { return NewEvent(ID, Done, "Removed") }