github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/errors.go (about)

     1  package golangsdk
     2  
     3  import "fmt"
     4  
     5  // BaseError is an error type that all other error types embed.
     6  type BaseError struct {
     7  	DefaultErrString string
     8  	Info             string
     9  }
    10  
    11  func (e BaseError) Error() string {
    12  	e.DefaultErrString = "An error occurred while executing a Gophercloud request."
    13  	return e.choseErrString()
    14  }
    15  
    16  func (e BaseError) choseErrString() string {
    17  	if e.Info != "" {
    18  		return e.Info
    19  	}
    20  	return e.DefaultErrString
    21  }
    22  
    23  // ErrMissingInput is the error when input is required in a particular
    24  // situation but not provided by the user
    25  type ErrMissingInput struct {
    26  	BaseError
    27  	Argument string
    28  }
    29  
    30  func (e ErrMissingInput) Error() string {
    31  	e.DefaultErrString = fmt.Sprintf("Missing input for argument [%s]", e.Argument)
    32  	return e.choseErrString()
    33  }
    34  
    35  // ErrInvalidInput is an error type used for most non-HTTP Gophercloud errors.
    36  type ErrInvalidInput struct {
    37  	ErrMissingInput
    38  	Value interface{}
    39  }
    40  
    41  func (e ErrInvalidInput) Error() string {
    42  	e.DefaultErrString = fmt.Sprintf("Invalid input provided for argument [%s]: [%+v]", e.Argument, e.Value)
    43  	return e.choseErrString()
    44  }
    45  
    46  // ErrUnexpectedResponseCode is returned by the Request method when a response code other than
    47  // those listed in OkCodes is encountered.
    48  type ErrUnexpectedResponseCode struct {
    49  	BaseError
    50  	URL      string
    51  	Method   string
    52  	Expected []int
    53  	Actual   int
    54  	Body     []byte
    55  }
    56  
    57  func (e ErrUnexpectedResponseCode) Error() string {
    58  	e.DefaultErrString = fmt.Sprintf(
    59  		"Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s",
    60  		e.Expected, e.Method, e.URL, e.Actual, e.Body,
    61  	)
    62  	return e.choseErrString()
    63  }
    64  
    65  // ErrDefault400 is the default error type returned on a 400 HTTP response code.
    66  type ErrDefault400 struct {
    67  	ErrUnexpectedResponseCode
    68  }
    69  
    70  // ErrDefault401 is the default error type returned on a 401 HTTP response code.
    71  type ErrDefault401 struct {
    72  	ErrUnexpectedResponseCode
    73  }
    74  
    75  // ErrDefault403 is the default error type returned on a 403 HTTP response code.
    76  type ErrDefault403 struct {
    77  	ErrUnexpectedResponseCode
    78  }
    79  
    80  // ErrDefault404 is the default error type returned on a 404 HTTP response code.
    81  type ErrDefault404 struct {
    82  	ErrUnexpectedResponseCode
    83  }
    84  
    85  // ErrDefault405 is the default error type returned on a 405 HTTP response code.
    86  type ErrDefault405 struct {
    87  	ErrUnexpectedResponseCode
    88  }
    89  
    90  // ErrDefault408 is the default error type returned on a 408 HTTP response code.
    91  type ErrDefault408 struct {
    92  	ErrUnexpectedResponseCode
    93  }
    94  
    95  // ErrDefault409 is the default error type returned on a 409 HTTP response code.
    96  type ErrDefault409 struct {
    97  	ErrUnexpectedResponseCode
    98  }
    99  
   100  // ErrDefault429 is the default error type returned on a 429 HTTP response code.
   101  type ErrDefault429 struct {
   102  	ErrUnexpectedResponseCode
   103  }
   104  
   105  // ErrDefault500 is the default error type returned on a 500 HTTP response code.
   106  type ErrDefault500 struct {
   107  	ErrUnexpectedResponseCode
   108  }
   109  
   110  // ErrDefault503 is the default error type returned on a 503 HTTP response code.
   111  type ErrDefault503 struct {
   112  	ErrUnexpectedResponseCode
   113  }
   114  
   115  func (e ErrDefault400) Error() string {
   116  	e.DefaultErrString = fmt.Sprintf(
   117  		"Bad request with: [%s %s], error message: %s",
   118  		e.Method, e.URL, e.Body,
   119  	)
   120  	return e.choseErrString()
   121  }
   122  func (e ErrDefault401) Error() string {
   123  	e.DefaultErrString = fmt.Sprintf(
   124  		"Authentication Failed, error message: %s", e.Body,
   125  	)
   126  	return e.choseErrString()
   127  }
   128  func (e ErrDefault403) Error() string {
   129  	e.DefaultErrString = fmt.Sprintf(
   130  		"Action Forbidden, error message: %s", e.Body,
   131  	)
   132  	return e.choseErrString()
   133  }
   134  func (e ErrDefault404) Error() string {
   135  	e.DefaultErrString = fmt.Sprintf(
   136  		"Resource not found: [%s %s], error message: %s",
   137  		e.Method, e.URL, e.Body,
   138  	)
   139  	return e.choseErrString()
   140  }
   141  func (e ErrDefault405) Error() string {
   142  	return "Method not allowed"
   143  }
   144  func (e ErrDefault408) Error() string {
   145  	return "The server timed out waiting for the request"
   146  }
   147  func (e ErrDefault429) Error() string {
   148  	return "Too many requests have been sent in a given amount of time. Pause" +
   149  		" requests, wait up to one minute, and try again."
   150  }
   151  func (e ErrDefault500) Error() string {
   152  	return "Internal Server Error"
   153  }
   154  func (e ErrDefault503) Error() string {
   155  	return "The service is currently unable to handle the request due to a temporary" +
   156  		" overloading or maintenance. This is a temporary condition. Try again later."
   157  }
   158  
   159  // Err400er is the interface resource error types implement to override the error message
   160  // from a 400 error.
   161  type Err400er interface {
   162  	Error400(ErrUnexpectedResponseCode) error
   163  }
   164  
   165  // Err401er is the interface resource error types implement to override the error message
   166  // from a 401 error.
   167  type Err401er interface {
   168  	Error401(ErrUnexpectedResponseCode) error
   169  }
   170  
   171  // Err403er is the interface resource error types implement to override the error message
   172  // from a 403 error.
   173  type Err403er interface {
   174  	Error403(ErrUnexpectedResponseCode) error
   175  }
   176  
   177  // Err404er is the interface resource error types implement to override the error message
   178  // from a 404 error.
   179  type Err404er interface {
   180  	Error404(ErrUnexpectedResponseCode) error
   181  }
   182  
   183  // Err405er is the interface resource error types implement to override the error message
   184  // from a 405 error.
   185  type Err405er interface {
   186  	Error405(ErrUnexpectedResponseCode) error
   187  }
   188  
   189  // Err408er is the interface resource error types implement to override the error message
   190  // from a 408 error.
   191  type Err408er interface {
   192  	Error408(ErrUnexpectedResponseCode) error
   193  }
   194  
   195  // Err409er is the interface resource error types implement to override the error message
   196  // from a 409 error.
   197  type Err409er interface {
   198  	Error409(ErrUnexpectedResponseCode) error
   199  }
   200  
   201  // Err429er is the interface resource error types implement to override the error message
   202  // from a 429 error.
   203  type Err429er interface {
   204  	Error429(ErrUnexpectedResponseCode) error
   205  }
   206  
   207  // Err500er is the interface resource error types implement to override the error message
   208  // from a 500 error.
   209  type Err500er interface {
   210  	Error500(ErrUnexpectedResponseCode) error
   211  }
   212  
   213  // Err503er is the interface resource error types implement to override the error message
   214  // from a 503 error.
   215  type Err503er interface {
   216  	Error503(ErrUnexpectedResponseCode) error
   217  }
   218  
   219  // ErrTimeOut is the error type returned when an operations times out.
   220  type ErrTimeOut struct {
   221  	BaseError
   222  }
   223  
   224  func (e ErrTimeOut) Error() string {
   225  	e.DefaultErrString = "A time out occurred"
   226  	return e.choseErrString()
   227  }
   228  
   229  // ErrUnableToReauthenticate is the error type returned when reauthentication fails.
   230  type ErrUnableToReauthenticate struct {
   231  	BaseError
   232  	ErrOriginal error
   233  }
   234  
   235  func (e ErrUnableToReauthenticate) Error() string {
   236  	e.DefaultErrString = fmt.Sprintf("Unable to re-authenticate: %s", e.ErrOriginal)
   237  	return e.choseErrString()
   238  }
   239  
   240  // ErrErrorAfterReauthentication is the error type returned when reauthentication
   241  // succeeds, but an error occurs afterword (usually an HTTP error).
   242  type ErrErrorAfterReauthentication struct {
   243  	BaseError
   244  	ErrOriginal error
   245  }
   246  
   247  func (e ErrErrorAfterReauthentication) Error() string {
   248  	e.DefaultErrString = fmt.Sprintf("Successfully re-authenticated, but got error executing request: %s", e.ErrOriginal)
   249  	return e.choseErrString()
   250  }
   251  
   252  // ErrServiceNotFound is returned when no service in a service catalog matches
   253  // the provided EndpointOpts. This is generally returned by provider service
   254  // factory methods like "NewComputeV2()" and can mean that a service is not
   255  // enabled for your account.
   256  type ErrServiceNotFound struct {
   257  	BaseError
   258  }
   259  
   260  func (e ErrServiceNotFound) Error() string {
   261  	e.DefaultErrString = "No suitable service could be found in the service catalog."
   262  	return e.choseErrString()
   263  }
   264  
   265  // ErrEndpointNotFound is returned when no available endpoints match the
   266  // provided EndpointOpts. This is also generally returned by provider service
   267  // factory methods, and usually indicates that a region was specified
   268  // incorrectly.
   269  type ErrEndpointNotFound struct {
   270  	BaseError
   271  }
   272  
   273  func (e ErrEndpointNotFound) Error() string {
   274  	e.DefaultErrString = "No suitable endpoint could be found in the service catalog."
   275  	return e.choseErrString()
   276  }
   277  
   278  // ErrResourceNotFound is the error when trying to retrieve a resource's
   279  // ID by name and the resource doesn't exist.
   280  type ErrResourceNotFound struct {
   281  	BaseError
   282  	Name         string
   283  	ResourceType string
   284  }
   285  
   286  func (e ErrResourceNotFound) Error() string {
   287  	e.DefaultErrString = fmt.Sprintf("Unable to find %s with name %s", e.ResourceType, e.Name)
   288  	return e.choseErrString()
   289  }
   290  
   291  // ErrMultipleResourcesFound is the error when trying to retrieve a resource's
   292  // ID by name and multiple resources have the user-provided name.
   293  type ErrMultipleResourcesFound struct {
   294  	BaseError
   295  	Name         string
   296  	Count        int
   297  	ResourceType string
   298  }
   299  
   300  func (e ErrMultipleResourcesFound) Error() string {
   301  	e.DefaultErrString = fmt.Sprintf("Found %d %ss matching %s", e.Count, e.ResourceType, e.Name)
   302  	return e.choseErrString()
   303  }
   304  
   305  // ErrUnexpectedType is the error when an unexpected type is encountered
   306  type ErrUnexpectedType struct {
   307  	BaseError
   308  	Expected string
   309  	Actual   string
   310  }
   311  
   312  func (e ErrUnexpectedType) Error() string {
   313  	e.DefaultErrString = fmt.Sprintf("Expected %s but got %s", e.Expected, e.Actual)
   314  	return e.choseErrString()
   315  }
   316  
   317  func unacceptedAttributeErr(attribute string) string {
   318  	return fmt.Sprintf("The base Identity V3 API does not accept authentication by %s", attribute)
   319  }
   320  
   321  func redundantWithTokenErr(attribute string) string {
   322  	return fmt.Sprintf("%s may not be provided when authenticating with a TokenID", attribute)
   323  }
   324  
   325  // ErrAPIKeyProvided indicates that an APIKey was provided but can't be used.
   326  type ErrAPIKeyProvided struct{ BaseError }
   327  
   328  func (e ErrAPIKeyProvided) Error() string {
   329  	return unacceptedAttributeErr("APIKey")
   330  }
   331  
   332  // ErrTenantIDProvided indicates that a TenantID was provided but can't be used.
   333  type ErrTenantIDProvided struct{ BaseError }
   334  
   335  func (e ErrTenantIDProvided) Error() string {
   336  	return unacceptedAttributeErr("TenantID")
   337  }
   338  
   339  // ErrTenantNameProvided indicates that a TenantName was provided but can't be used.
   340  type ErrTenantNameProvided struct{ BaseError }
   341  
   342  func (e ErrTenantNameProvided) Error() string {
   343  	return unacceptedAttributeErr("TenantName")
   344  }
   345  
   346  // ErrUsernameWithToken indicates that a Username was provided, but token authentication is being used instead.
   347  type ErrUsernameWithToken struct{ BaseError }
   348  
   349  func (e ErrUsernameWithToken) Error() string {
   350  	return redundantWithTokenErr("Username")
   351  }
   352  
   353  // ErrUserIDWithToken indicates that a UserID was provided, but token authentication is being used instead.
   354  type ErrUserIDWithToken struct{ BaseError }
   355  
   356  func (e ErrUserIDWithToken) Error() string {
   357  	return redundantWithTokenErr("UserID")
   358  }
   359  
   360  // ErrDomainIDWithToken indicates that a DomainID was provided, but token authentication is being used instead.
   361  type ErrDomainIDWithToken struct{ BaseError }
   362  
   363  func (e ErrDomainIDWithToken) Error() string {
   364  	return redundantWithTokenErr("DomainID")
   365  }
   366  
   367  // ErrDomainNameWithToken indicates that a DomainName was provided, but token authentication is being used instead.s
   368  type ErrDomainNameWithToken struct{ BaseError }
   369  
   370  func (e ErrDomainNameWithToken) Error() string {
   371  	return redundantWithTokenErr("DomainName")
   372  }
   373  
   374  // ErrUsernameOrUserID indicates that neither username nor userID are specified, or both are at once.
   375  type ErrUsernameOrUserID struct{ BaseError }
   376  
   377  func (e ErrUsernameOrUserID) Error() string {
   378  	return "Exactly one of Username and UserID must be provided for password authentication"
   379  }
   380  
   381  // ErrDomainIDOrDomainName indicates that a username was provided, but no domain to scope it.
   382  // It may also indicate that both a DomainID and a DomainName were provided at once.
   383  type ErrDomainIDOrDomainName struct{ BaseError }
   384  
   385  func (e ErrDomainIDOrDomainName) Error() string {
   386  	return "You must provide exactly one of DomainID or DomainName to authenticate by Username"
   387  }
   388  
   389  // ErrMissingPassword indicates that no password was provided and no token is available.
   390  type ErrMissingPassword struct{ BaseError }
   391  
   392  func (e ErrMissingPassword) Error() string {
   393  	return "You must provide a password to authenticate"
   394  }
   395  
   396  // ErrScopeDomainIDOrDomainName indicates that a domain ID or Name was required in a Scope, but not present.
   397  type ErrScopeDomainIDOrDomainName struct{ BaseError }
   398  
   399  func (e ErrScopeDomainIDOrDomainName) Error() string {
   400  	return "You must provide exactly one of DomainID or DomainName in a Scope with ProjectName"
   401  }
   402  
   403  // ErrScopeProjectIDOrProjectName indicates that both a ProjectID and a ProjectName were provided in a Scope.
   404  type ErrScopeProjectIDOrProjectName struct{ BaseError }
   405  
   406  func (e ErrScopeProjectIDOrProjectName) Error() string {
   407  	return "You must provide at most one of ProjectID or ProjectName in a Scope"
   408  }
   409  
   410  // ErrScopeProjectIDAlone indicates that a ProjectID was provided with other constraints in a Scope.
   411  type ErrScopeProjectIDAlone struct{ BaseError }
   412  
   413  func (e ErrScopeProjectIDAlone) Error() string {
   414  	return "ProjectID must be supplied alone in a Scope"
   415  }
   416  
   417  // ErrScopeEmpty indicates that no credentials were provided in a Scope.
   418  type ErrScopeEmpty struct{ BaseError }
   419  
   420  func (e ErrScopeEmpty) Error() string {
   421  	return "You must provide either a Project or Domain in a Scope"
   422  }
   423  
   424  type ErrUserIDNotFound struct{ BaseError }
   425  
   426  func (e ErrUserIDNotFound) Error() string {
   427  	return "You must provide UserID for MFA"
   428  }