github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/errors.go (about)

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