Skip to content

Commit

Permalink
fix: spinner wraps original error (#1029)
Browse files Browse the repository at this point in the history
* refactor(compute/deploy): convert IfElse to switch

* fix: spinner error wraps original error

* refactor: linter corrections

* refactor: use WrapErr abstraction

* fix: move wrapped underlying error up so spinner can wrap it

* refactor: clean up conditionals

* fix: remove WrapErr abstractions
  • Loading branch information
Integralist authored Oct 6, 2023
1 parent 349975b commit e1e6d55
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 122 deletions.
25 changes: 14 additions & 11 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ func manageNoServiceIDFlow(
var serviceName string

// The service name will be whatever is set in the --service-name flag.
// If the flag isn't set, and we're able to prompt, we'll ask the user.
// If the flag isn't set, and we're non-interactive, we'll use the default.
// If the flag isn't set, and we're interactive, we'll prompt the user.
switch {
case serviceNameFlag.WasSet:
serviceName = serviceNameFlag.Value
Expand Down Expand Up @@ -633,28 +633,28 @@ func createService(
if strings.Contains(err.Error(), trialNotActivated) {
user, err := apiClient.GetCurrentUser()
if err != nil {
err = fmt.Errorf("unable to identify user associated with the given token: %w", err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return "", nil, spinErr
return "", nil, fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return serviceID, serviceVersion, fsterr.RemediationError{
Inner: fmt.Errorf("unable to identify user associated with the given token: %w", err),
Inner: err,
Remediation: "To ensure you have access to the Compute@Edge platform we need your Customer ID. " + fsterr.AuthRemediation,
}
}

err = fnActivateTrial(user.CustomerID)
if err != nil {
err = fmt.Errorf("error creating service: you do not have the Compute@Edge free trial enabled on your Fastly account")
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return "", nil, spinErr
return "", nil, fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return serviceID, serviceVersion, fsterr.RemediationError{
Inner: fmt.Errorf("error creating service: you do not have the Compute@Edge free trial enabled on your Fastly account"),
Inner: err,
Remediation: fsterr.ComputeTrialRemediation,
}
}
Expand Down Expand Up @@ -1199,14 +1199,15 @@ func checkingServiceAvailability(
for {
select {
case <-timeout:
err := errors.New("timeout: service not yet available")
returnedStatus := fmt.Sprintf(" (status: %d)", status)
spinner.StopFailMessage(msg + returnedStatus)
spinErr := spinner.StopFail()
if spinErr != nil {
return status, spinErr
return status, fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return status, fsterr.RemediationError{
Inner: errors.New("service not yet available"),
Inner: err,
Remediation: fmt.Sprintf(remediation, "timed out", expected, status),
}
case t := <-ticker.C:
Expand All @@ -1219,17 +1220,19 @@ func checkingServiceAvailability(
// and success scenarios.
ok, status, err = pingServiceURL(serviceURL, c.Globals.HTTPClient, c.StatusCheckCode)
if err != nil {
err := fmt.Errorf("failed to ping service URL: %w", err)
returnedStatus := fmt.Sprintf(" (status: %d)", status)
spinner.StopFailMessage(msg + returnedStatus)
spinErr := spinner.StopFail()
if spinErr != nil {
return status, spinErr
return status, fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return status, fsterr.RemediationError{
Inner: err,
Remediation: fmt.Sprintf(remediation, "failed", expected, status),
}
} else if ok {
}
if ok {
returnedStatus := fmt.Sprintf(" (status: %d)", status)
spinner.StopMessage(msg + returnedStatus)
return status, spinner.Stop()
Expand Down
68 changes: 28 additions & 40 deletions pkg/commands/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,15 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
// But we can't just call StopFailMessage() without first starting the spinner.
if c.Globals.Flags.Verbose {
text.Break(out)
err := spinner.Start()
if err != nil {
return err
spinErr := spinner.Start()
if spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
spinner.Message(msg + "...")

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
spinErr = spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
}
return err
Expand Down Expand Up @@ -683,47 +682,44 @@ func fetchPackageTemplate(

// If the user has provided a local file path, we'll recursively copy the
// directory to c.dir.
fi, err := os.Stat(c.cloneFrom)
if err != nil {
c.Globals.ErrLog.Add(err)
} else if fi.IsDir() {
if fi, err := os.Stat(c.cloneFrom); err == nil && fi.IsDir() {
if err := cp.Copy(c.cloneFrom, c.dir); err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
}

spinner.StopMessage(msg)
return spinner.Stop()
}
c.Globals.ErrLog.Add(err)

req, err := http.NewRequest("GET", c.cloneFrom, nil)
if err != nil {
err = fmt.Errorf("failed to construct package request URL: %w", err)
c.Globals.ErrLog.Add(err)

if gitRepositoryRegEx.MatchString(c.cloneFrom) {
if err := clonePackageFromEndpoint(c.cloneFrom, branch, tag, c.dir); err != nil {
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
}

spinner.StopMessage(msg)
return spinner.Stop()
}

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return fmt.Errorf("failed to construct package request URL: %w", err)
return err
}

for _, archive := range archives {
Expand All @@ -734,28 +730,25 @@ func fetchPackageTemplate(

res, err := c.Globals.HTTPClient.Do(req)
if err != nil {
err = fmt.Errorf("failed to get package: %w", err)
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return fmt.Errorf("failed to get package: %w", err)
return err
}
defer res.Body.Close() // #nosec G307

if res.StatusCode != http.StatusOK {
err := fmt.Errorf("failed to get package: %s", res.Status)
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return err
}

Expand All @@ -769,15 +762,14 @@ func fetchPackageTemplate(
/* #nosec */
f, err := os.Create(filename)
if err != nil {
err = fmt.Errorf("failed to create local %s archive: %w", filename, err)
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return fmt.Errorf("failed to create local %s archive: %w", filename, err)
return err
}
defer func() {
// NOTE: Later on we rename the file to include an extension and the
Expand All @@ -792,15 +784,14 @@ func fetchPackageTemplate(

_, err = io.Copy(f, res.Body)
if err != nil {
err = fmt.Errorf("failed to write %s archive to disk: %w", filename, err)
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return fmt.Errorf("failed to write %s archive to disk: %w", filename, err)
return err
}

// NOTE: We used to `defer` the closing of the file after its creation but
Expand Down Expand Up @@ -844,13 +835,11 @@ mimes:
err := os.Rename(filename, filenameWithExt)
if err != nil {
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return err
}
filename = filenameWithExt
Expand All @@ -861,15 +850,14 @@ mimes:

err = archive.Extract()
if err != nil {
err = fmt.Errorf("failed to extract %s archive content: %w", filename, err)
c.Globals.ErrLog.Add(err)

spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return fmt.Errorf("failed to extract %s archive content: %w", filename, err)
return err
}

spinner.StopMessage(msg)
Expand All @@ -880,7 +868,7 @@ mimes:
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return err
}
Expand Down
22 changes: 10 additions & 12 deletions pkg/commands/compute/language_toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ func (bt BuildToolchain) Build() error {
// But we can't just call StopFailMessage() without first starting the spinner.
if bt.verbose {
text.Break(bt.out)
err := bt.spinner.Start()
if err != nil {
return err
spinErr := bt.spinner.Start()
if spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
bt.spinner.Message(msg + "...")

bt.spinner.StopFailMessage(msg)
spinErr := bt.spinner.StopFail()
spinErr = bt.spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
}
// WARNING: Don't try to add 'StopFailMessage/StopFail' calls here.
Expand Down Expand Up @@ -193,16 +192,15 @@ func (bt BuildToolchain) Build() error {
// But we can't just call StopFailMessage() without first starting the spinner.
if bt.verbose {
text.Break(bt.out)
err := bt.spinner.Start()
if err != nil {
return err
spinErr := bt.spinner.Start()
if spinErr != nil {
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
bt.spinner.Message(msg + "...")

bt.spinner.StopFailMessage(msg)
spinErr := bt.spinner.StopFail()
spinErr = bt.spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
}
// WARNING: Don't try to add 'StopFailMessage/StopFail' calls here.
Expand Down
21 changes: 12 additions & 9 deletions pkg/commands/compute/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ func installViceroy(

latestVersion, err = av.LatestVersion()
if err != nil {
err = fmt.Errorf("error fetching latest version: %w", err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}

return fsterr.RemediationError{
Inner: fmt.Errorf("error fetching latest version: %w", err),
Inner: err,
Remediation: fsterr.NetworkRemediation,
}
}
Expand Down Expand Up @@ -482,23 +482,26 @@ func installViceroy(
}

if err != nil {
err = fmt.Errorf("error downloading Viceroy release: %w", err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return fmt.Errorf("error downloading Viceroy release: %w", err)
return err
}
defer os.RemoveAll(tmpBin)

if err := os.Rename(tmpBin, bin); err != nil {
if err := filesystem.CopyFile(tmpBin, bin); err != nil {
err = fmt.Errorf("failed to rename/move file: %w", err)
if copyErr := filesystem.CopyFile(tmpBin, bin); copyErr != nil {
err = fmt.Errorf("failed to copy file: %w (original error: %w)", copyErr, err)
spinner.StopFailMessage(msg)
spinErr := spinner.StopFail()
if spinErr != nil {
return spinErr
return fmt.Errorf(text.SpinnerErrWrapper, spinErr, err)
}
return fmt.Errorf("error moving latest Viceroy binary in place: %w", err)
return err
}
}

Expand Down Expand Up @@ -789,7 +792,7 @@ func ignoreFiles(watchDir cmd.OptionalString) *ignore.GitIgnore {
if watchDir.WasSet {
root = watchDir.Value
if !strings.HasPrefix(root, "/") {
root = root + "/"
root += "/"
}
}

Expand Down
Loading

0 comments on commit e1e6d55

Please sign in to comment.