github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/updater/error.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package updater
     5  
     6  import "fmt"
     7  
     8  // ErrorType is a unique short string denoting the error category
     9  type ErrorType string
    10  
    11  const (
    12  	// UnknownError is for if we had an unknown error
    13  	UnknownError ErrorType = "unknown"
    14  	// CancelError is for if we canceled
    15  	CancelError ErrorType = "cancel"
    16  	// ConfigError is for errors reading/saving config
    17  	ConfigError ErrorType = "config"
    18  	// ConfigError is for when the GUI is active
    19  	GUIBusyError ErrorType = "guiBusy"
    20  )
    21  
    22  // Errors corresponding to each stage in the update process
    23  const (
    24  	// FindError is an error trying to find the update
    25  	FindError ErrorType = "find"
    26  	// PromptError is an UI prompt error
    27  	PromptError ErrorType = "prompt"
    28  	// DownloadError is an error trying to download the update
    29  	DownloadError ErrorType = "download"
    30  	// ApplyError is an error applying the update
    31  	ApplyError ErrorType = "apply"
    32  	// VerifyError is an error verifing the update (signature or digest)
    33  	VerifyError ErrorType = "verify"
    34  )
    35  
    36  func (t ErrorType) String() string {
    37  	return string(t)
    38  }
    39  
    40  // Error is an update error with a type/category for reporting
    41  type Error struct {
    42  	errorType ErrorType
    43  	source    error
    44  }
    45  
    46  // NewError constructs an Error from a source error
    47  func NewError(errorType ErrorType, err error) Error {
    48  	return Error{errorType: errorType, source: err}
    49  }
    50  
    51  // TypeString returns a unique short string to denote the error type
    52  func (e Error) TypeString() string {
    53  	return e.errorType.String()
    54  }
    55  
    56  // IsCancel returns true if error was from a cancel
    57  func (e Error) IsCancel() bool {
    58  	return e.errorType == CancelError
    59  }
    60  
    61  // IsGUIBusy returns true if the UI was active
    62  func (e Error) IsGUIBusy() bool {
    63  	return e.errorType == GUIBusyError
    64  }
    65  
    66  // Error returns description for an UpdateError
    67  func (e Error) Error() string {
    68  	if e.source == nil {
    69  		return fmt.Sprintf("Update Error (%s)", e.TypeString())
    70  	}
    71  	return fmt.Sprintf("Update Error (%s): %s", e.TypeString(), e.source.Error())
    72  }
    73  
    74  // CancelErr can be returned by lifecycle methods to abort an update
    75  func CancelErr(err error) Error {
    76  	return NewError(CancelError, err)
    77  }
    78  
    79  func guiBusyErr(err error) Error {
    80  	return NewError(GUIBusyError, err)
    81  }
    82  
    83  func promptErr(err error) Error {
    84  	return NewError(PromptError, err)
    85  }
    86  
    87  func findErr(err error) Error {
    88  	return NewError(FindError, err)
    89  }
    90  
    91  func downloadErr(err error) Error {
    92  	return NewError(DownloadError, err)
    93  }
    94  
    95  func verifyErr(err error) Error {
    96  	return NewError(VerifyError, err)
    97  }
    98  
    99  func applyErr(err error) Error {
   100  	return NewError(ApplyError, err)
   101  }
   102  
   103  func configErr(err error) Error {
   104  	return NewError(ConfigError, err)
   105  }