github.com/fluxcd/go-git-providers@v0.19.3/gitprovider/errors.go (about) 1 /* 2 Copyright 2020 The Flux CD contributors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gitprovider 18 19 import ( 20 "errors" 21 "fmt" 22 "net/http" 23 "time" 24 ) 25 26 var ( 27 // ErrNoProviderSupport describes that the provider doesn't support the requested feature. 28 ErrNoProviderSupport = errors.New("no provider support for this feature") 29 // ErrDomainUnsupported describes the case where e.g. a GitHub provider used for trying to get 30 // information from e.g. "gitlab.com". 31 ErrDomainUnsupported = errors.New("the client doesn't support handling requests for this domain") 32 33 // ErrNotTopLevelOrganization describes the case where it's mandatory to specify a top-level organization 34 // (e.g. to access teams), but a sub-organization was passed as the OrganizationRef. 35 ErrNotTopLevelOrganization = errors.New("expected top-level organization, received sub-organization instead") 36 // ErrInvalidArgument describes a generic error where an invalid argument have been specified to a function. 37 ErrInvalidArgument = errors.New("invalid argument specified") 38 // ErrUnexpectedEvent describes a case where something really unexpected happened in the program. 39 ErrUnexpectedEvent = errors.New("an unexpected error occurred") 40 41 // ErrAlreadyExists is returned by .Create() requests if the given resource already exists. 42 // Use .Reconcile() instead if you want to idempotently create the resource. 43 ErrAlreadyExists = errors.New("resource already exists, cannot create object. Use Reconcile() to create it idempotently") 44 // ErrNotFound is returned by .Get() and .Update() calls if the given resource doesn't exist. 45 ErrNotFound = errors.New("the requested resource was not found") 46 // ErrInvalidServerData is returned when the server returned invalid data, e.g. missing required fields in the response. 47 ErrInvalidServerData = errors.New("got invalid data from server, don't know how to handle") 48 49 // ErrURLUnsupportedScheme is returned if an URL without the HTTPS scheme is parsed. 50 ErrURLUnsupportedScheme = errors.New("unsupported URL scheme, only HTTPS supported") 51 // ErrURLUnsupportedParts is returned if an URL with fragment, query values and/or user information is parsed. 52 ErrURLUnsupportedParts = errors.New("URL cannot have fragments, query values nor user information") 53 // ErrURLInvalid is returned if an URL is invalid when parsing. 54 ErrURLInvalid = errors.New("invalid organization, user or repository URL") 55 // ErrURLMissingRepoName is returned if there is no repository name in the URL. 56 ErrURLMissingRepoName = errors.New("missing repository name") 57 58 // ErrInvalidClientOptions is the error returned when calling NewClient() with 59 // invalid options (e.g. specifying mutually exclusive options). 60 ErrInvalidClientOptions = errors.New("invalid options given to NewClient()") 61 // ErrDestructiveCallDisallowed happens when the client isn't set up with WithDestructiveAPICalls() 62 // but a destructive action is called. 63 ErrDestructiveCallDisallowed = errors.New("destructive call was blocked, disallowed by client") 64 // ErrInvalidTransportChainReturn is returned if a ChainableRoundTripperFunc returns nil, which is invalid. 65 ErrInvalidTransportChainReturn = errors.New("the return value of a ChainableRoundTripperFunc must not be nil") 66 67 // ErrInvalidPermissionLevel is the error returned when there is no mapping 68 // from the given level to the gitprovider levels. 69 ErrInvalidPermissionLevel = errors.New("invalid permission level") 70 // ErrMissingHeader is returned when an expected header is missing from the HTTP response. 71 ErrMissingHeader = errors.New("header is missing") 72 // ErrGroupNotFound is returned when the gitlab group does not exist 73 ErrGroupNotFound = errors.New("404 Group Not Found") 74 ) 75 76 // HTTPError is an error that contains context about the HTTP request/response that failed. 77 type HTTPError struct { 78 // HTTP response that caused this error. 79 Response *http.Response `json:"-"` 80 // Full error message, human-friendly and formatted. 81 ErrorMessage string `json:"errorMessage"` 82 // Message about what happened. 83 Message string `json:"message"` 84 // Where to find more information about the error. 85 DocumentationURL string `json:"documentationURL"` 86 } 87 88 // Error implements the error interface. 89 func (e *HTTPError) Error() string { 90 return e.ErrorMessage 91 } 92 93 // RateLimitError is an error, extending HTTPError, that contains context about rate limits. 94 type RateLimitError struct { 95 // RateLimitError extends HTTPError. 96 HTTPError `json:",inline"` 97 98 // The number of requests per hour the client is currently limited to. 99 Limit int `json:"limit"` 100 // The number of remaining requests the client can make this hour. 101 Remaining int `json:"remaining"` 102 // The timestamp at which point the current rate limit will reset. 103 Reset time.Time `json:"reset"` 104 } 105 106 // ValidationError is an error, extending HTTPError, that contains context about failed server-side validation. 107 type ValidationError struct { 108 // RateLimitError extends HTTPError. 109 HTTPError `json:",inline"` 110 111 // Errors contain context about what validation(s) failed. 112 Errors []ValidationErrorItem `json:"errors"` 113 } 114 115 // ValidationErrorItem represents a single invalid field in an invalid request. 116 type ValidationErrorItem struct { 117 // Resource on which the error occurred. 118 Resource string `json:"resource"` 119 // Field on which the error occurred. 120 Field string `json:"field"` 121 // Code for the validation error. 122 Code string `json:"code"` 123 // Message describing the error. Errors with Code == "custom" will always have this set. 124 Message string `json:"message"` 125 } 126 127 // InvalidCredentialsError describes that that the request login credentials (e.g. an Oauth2 token) 128 // was invalid (i.e. a 401 Unauthorized or 403 Forbidden status was returned). This does NOT mean that 129 // "the login was successful but you don't have permission to access this resource". In that case, a 130 // 404 Not Found error would be returned. 131 type InvalidCredentialsError struct { 132 // InvalidCredentialsError extends HTTPError. 133 HTTPError `json:",inline"` 134 } 135 136 // ErrIncorrectUser describes that the user provided was incorrect 137 // 138 // It is returned by `UserRepositories().Create` when an incorrect UserLogin is passed in 139 type ErrIncorrectUser struct { 140 user string 141 } 142 143 // Error implements the error interface. 144 func NewErrIncorrectUser(user string) *ErrIncorrectUser { 145 return &ErrIncorrectUser{user} 146 } 147 148 // Error implements the error interface. 149 func (e *ErrIncorrectUser) Error() string { 150 return fmt.Sprintf("incorrect user '%s' provided", e.user) 151 }