Adding error progress indication when errors

Signed-off-by: Guillame Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillame Tardif 2020-11-27 18:18:14 +01:00
parent 7ddd8e5e97
commit 4d1f265c62
6 changed files with 30 additions and 21 deletions

View File

@ -116,6 +116,7 @@ func createOrUpdateACIContainers(ctx context.Context, aciContext store.AciContex
groupDefinition, groupDefinition,
) )
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(groupDisplay))
return err return err
} }

View File

@ -87,12 +87,14 @@ func (cs *aciVolumeService) Create(ctx context.Context, name string, options int
w.Event(progress.NewEvent(opts.Account, progress.Working, "Validating")) w.Event(progress.NewEvent(opts.Account, progress.Working, "Validating"))
accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID) accountClient, err := login.NewStorageAccountsClient(cs.aciContext.SubscriptionID)
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(opts.Account))
return volumes.Volume{}, err return volumes.Volume{}, err
} }
account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "") account, err := accountClient.GetProperties(ctx, cs.aciContext.ResourceGroup, opts.Account, "")
if err == nil { if err == nil {
w.Event(progress.NewEvent(opts.Account, progress.Done, "Use existing")) w.Event(progress.NewEvent(opts.Account, progress.Done, "Use existing"))
} else if !account.HasHTTPStatus(http.StatusNotFound) { } else if !account.HasHTTPStatus(http.StatusNotFound) {
w.Event(progress.ErrorEvent(opts.Account))
return volumes.Volume{}, err return volumes.Volume{}, err
} else { } else {
result, err := accountClient.CheckNameAvailability(ctx, storage.AccountCheckNameAvailabilityParameters{ 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"), Type: to.StringPtr("Microsoft.Storage/storageAccounts"),
}) })
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(opts.Account))
return volumes.Volume{}, err return volumes.Volume{}, err
} }
if !*result.NameAvailable { if !*result.NameAvailable {
w.Event(progress.ErrorEvent(opts.Account))
return volumes.Volume{}, errors.New("error: " + *result.Message) return volumes.Volume{}, errors.New("error: " + *result.Message)
} }
parameters := defaultStorageAccountParams(cs.aciContext) 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) future, err := accountClient.Create(ctx, cs.aciContext.ResourceGroup, opts.Account, parameters)
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(opts.Account, "Error")) w.Event(progress.ErrorEvent(opts.Account))
return volumes.Volume{}, err return volumes.Volume{}, err
} }
if err := future.WaitForCompletionRef(ctx, accountClient.Client); err != nil { 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 return volumes.Volume{}, err
} }
account, err = future.Result(accountClient) account, err = future.Result(accountClient)
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(opts.Account, "Error")) w.Event(progress.ErrorEvent(opts.Account))
return volumes.Volume{}, err return volumes.Volume{}, err
} }
w.Event(progress.CreatedEvent(opts.Account)) 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, "") fileShare, err := fileShareClient.Get(ctx, cs.aciContext.ResourceGroup, *account.Name, name, "")
if err == nil { 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) return volumes.Volume{}, errors.Wrapf(errdefs.ErrAlreadyExists, "Azure fileshare %q already exists", name)
} }
if !fileShare.HasHTTPStatus(http.StatusNotFound) { if !fileShare.HasHTTPStatus(http.StatusNotFound) {
w.Event(progress.ErrorEvent(name, "Error")) w.Event(progress.ErrorEvent(name))
return volumes.Volume{}, err return volumes.Volume{}, err
} }
fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, name, storage.FileShare{}) fileShare, err = fileShareClient.Create(ctx, cs.aciContext.ResourceGroup, *account.Name, name, storage.FileShare{})
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(name, "Error")) w.Event(progress.ErrorEvent(name))
return volumes.Volume{}, err return volumes.Volume{}, err
} }
w.Event(progress.CreatedEvent(name)) w.Event(progress.CreatedEvent(name))

View File

@ -68,7 +68,7 @@ func doDelete(ctx context.Context, delete func(ctx context.Context, arn string)
w.Event(progress.RemovingEvent(r.LogicalID)) w.Event(progress.RemovingEvent(r.LogicalID))
err := delete(ctx, r.ARN) err := delete(ctx, r.ARN)
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(r.LogicalID, "Error")) w.Event(progress.ErrorEvent(r.LogicalID))
return err return err
} }
w.Event(progress.RemovedEvent(r.LogicalID)) w.Event(progress.RemovedEvent(r.LogicalID))

View File

@ -101,11 +101,7 @@ func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, op
} }
} }
} }
w.Event(progress.Event{ w.Event(progress.NewEvent(resource, progressStatus, fmt.Sprintf("%s %s", toCamelCase(status), reason)))
ID: resource,
Status: progressStatus,
StatusText: fmt.Sprintf("%s %s", toCamelCase(status), reason),
})
} }
if operation != stackCreate || stackErr != nil { if operation != stackCreate || stackErr != nil {
continue continue
@ -116,7 +112,7 @@ func (b *ecsAPIService) WaitStackCompletion(ctx context.Context, name string, op
} }
stackErr = err stackErr = err
operation = stackDelete operation = stackDelete
w.Event(progress.ErrorEvent(name, err.Error())) w.Event(progress.ErrorMessageEvent(name, err.Error()))
} }
} }

View File

@ -120,11 +120,13 @@ func (s *composeService) Down(ctx context.Context, projectName string) error {
w.Event(progress.NewEvent(getContainerName(container), progress.Working, "Stopping")) w.Event(progress.NewEvent(getContainerName(container), progress.Working, "Stopping"))
err := s.apiClient.ContainerStop(ctx, container.ID, nil) err := s.apiClient.ContainerStop(ctx, container.ID, nil)
if err != nil { if err != nil {
w.Event(progress.ErrorMessageEvent(getContainerName(container), "Error while Stopping"))
return err return err
} }
w.Event(progress.RemovingEvent(getContainerName(container))) w.Event(progress.RemovingEvent(getContainerName(container)))
err = s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{}) err = s.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
if err != nil { if err != nil {
w.Event(progress.ErrorMessageEvent(getContainerName(container), "Error while Removing"))
return err return err
} }
w.Event(progress.RemovedEvent(getContainerName(container))) 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 := progress.ContextWriter(ctx)
w.Event(progress.CreatingEvent(networkEventName)) w.Event(progress.CreatingEvent(networkEventName))
if _, err := s.apiClient.NetworkCreate(ctx, n.Name, createOpts); err != nil { 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) return errors.Wrapf(err, "failed to create network %s", n.Name)
} }
w.Event(progress.CreatedEvent(networkEventName)) w.Event(progress.CreatedEvent(networkEventName))
@ -599,9 +602,8 @@ func (s *composeService) ensureNetworkDown(ctx context.Context, networkID string
w.Event(progress.RemovingEvent(eventName)) w.Event(progress.RemovingEvent(eventName))
if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil { if err := s.apiClient.NetworkRemove(ctx, networkID); err != nil {
msg := fmt.Sprintf("failed to create network %s", networkID) w.Event(progress.ErrorEvent(eventName))
w.Event(progress.ErrorEvent(eventName, "Error: "+msg)) return errors.Wrapf(err, fmt.Sprintf("failed to create network %s", networkID))
return errors.Wrapf(err, msg)
} }
w.Event(progress.RemovedEvent(eventName)) w.Event(progress.RemovedEvent(eventName))
@ -623,10 +625,11 @@ func (s *composeService) ensureVolume(ctx context.Context, volume types.VolumeCo
Driver: volume.Driver, Driver: volume.Driver,
DriverOpts: volume.DriverOpts, DriverOpts: volume.DriverOpts,
}) })
w.Event(progress.CreatedEvent(eventName))
if err != nil { if err != nil {
w.Event(progress.ErrorEvent(eventName))
return err return err
} }
w.Event(progress.CreatedEvent(eventName))
} }
return err return err
} }

View File

@ -42,11 +42,16 @@ type Event struct {
spinner *spinner spinner *spinner
} }
// ErrorEvent creates a new Error Event // ErrorMessageEvent creates a new Error Event with message
func ErrorEvent(ID string, msg string) Event { func ErrorMessageEvent(ID string, msg string) Event {
return NewEvent(ID, Error, msg) 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 // CreatingEvent creates a new Create in progress Event
func CreatingEvent(ID string) Event { func CreatingEvent(ID string) Event {
return NewEvent(ID, Working, "Creating...") return NewEvent(ID, Working, "Creating...")
@ -57,12 +62,12 @@ func CreatedEvent(ID string) Event {
return NewEvent(ID, Done, "Created") 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 { func RemovingEvent(ID string) Event {
return NewEvent(ID, Working, "Removing...") return NewEvent(ID, Working, "Removing...")
} }
// CreatedEvent creates a new Created (done) Event // RemovedEvent creates a new removed (done) Event
func RemovedEvent(ID string) Event { func RemovedEvent(ID string) Event {
return NewEvent(ID, Done, "Removed") return NewEvent(ID, Done, "Removed")
} }