consts.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httperrors
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. )
  18. const (
  19. ErrBadGateway = errors.Error("BadGateway")
  20. ErrNotImplemented = errors.ErrNotImplemented
  21. ErrInternalError = errors.Error("InternalServerError")
  22. ErrResourceNotReady = errors.Error("ResourceNotReadyError")
  23. ErrPayment = errors.Error("PaymentError")
  24. ErrImageNotFound = errors.Error("ImageNotFoundError")
  25. ErrResourceNotFound = errors.Error("ResourceNotFoundError")
  26. ErrSpecNotFound = errors.Error("SpecNotFoundError")
  27. ErrActionNotFound = errors.Error("ActionNotFoundError")
  28. ErrTenantNotFound = errors.Error("TenantNotFoundError")
  29. ErrServerStatus = errors.Error("ServerStatusError")
  30. ErrInvalidStatus = errors.ErrInvalidStatus
  31. ErrInvalidIdpStatus = errors.Error("InvalidIdpStatus")
  32. ErrInvalidFormat = errors.ErrInvalidFormat
  33. ErrInputParameter = errors.Error("InputParameterError")
  34. ErrWeakPassword = errors.Error("WeakPasswordError")
  35. ErrMissingParameter = errors.Error("MissingParameterError")
  36. ErrInsufficientResource = errors.Error("InsufficientResourceError")
  37. ErrOutOfResource = errors.Error("OutOfResource")
  38. ErrOutOfQuota = errors.Error("OutOfQuotaError")
  39. ErrOutOfRange = errors.Error("OutOfRange")
  40. ErrOutOfLimit = errors.Error("OutOfLimit")
  41. ErrNotSufficientPrivilege = errors.Error("NotSufficientPrivilegeError")
  42. ErrUnsupportedOperation = errors.Error("UnsupportOperationError")
  43. ErrNotSupported = errors.ErrNotSupported
  44. ErrNotEmpty = errors.Error("NotEmptyError")
  45. ErrBadRequest = errors.Error("BadRequestError")
  46. ErrEmptyRequest = errors.Error("EmptyRequestError")
  47. ErrUnauthorized = errors.Error("UnauthorizedError")
  48. ErrInvalidCredential = errors.Error("InvalidCredentialError")
  49. ErrUnauthenticated = ErrInvalidCredential
  50. ErrForbidden = errors.Error("ForbiddenError")
  51. ErrNotFound = errors.ErrNotFound
  52. ErrNotAcceptable = errors.Error("NotAcceptableError")
  53. ErrDuplicateName = errors.Error("DuplicateNameError")
  54. ErrDuplicateResource = errors.Error("DuplicateResourceError")
  55. ErrConflict = errors.Error("ConflictError")
  56. ErrDuplicateId = errors.ErrDuplicateId
  57. ErrResourceBusy = errors.Error("ResourceBusyError")
  58. ErrRequireLicense = errors.Error("RequireLicenseError")
  59. ErrTimeout = errors.ErrTimeout
  60. ErrProtectedResource = errors.Error("ProtectedResourceError")
  61. ErrNoProject = errors.Error("NoProjectError")
  62. ErrInvalidProvider = errors.Error("InvalidProvider")
  63. ErrNoBalancePermission = errors.Error("NoBalancePermission")
  64. ErrTooLarge = errors.Error("TooLargeEntity")
  65. ErrTooManyAttempts = errors.Error("TooManyFailedAttempts")
  66. ErrTooManyRequests = errors.Error("TooManyRequests")
  67. ErrUnsupportedProtocol = errors.ErrUnsupportedProtocol
  68. ErrPolicyDefinition = errors.Error("PolicyDefinitionError")
  69. ErrUserNotFound = errors.Error("UserNotFound")
  70. ErrUserLocked = errors.Error("UserLocked")
  71. ErrUserDisabled = errors.Error("UserDisabled")
  72. ErrUserExpired = errors.Error("UserExpired")
  73. ErrWrongPassword = errors.Error("WrongPassword")
  74. ErrIncorrectUsernameOrPassword = errors.Error("IncorrectUsernameOrPassword")
  75. ErrServiceAbnormal = errors.Error("ServiceAbnormal")
  76. ErrInvalidAccessKey = errors.Error("InvalidAccessKey")
  77. ErrNoPermission = errors.Error("NoPermission")
  78. )
  79. var (
  80. httpErrorCode = map[errors.Error]int{
  81. errors.ErrClient: 400,
  82. errors.ErrServer: 500,
  83. errors.ErrUnclassified: 500,
  84. ErrInvalidProvider: 400,
  85. ErrNoBalancePermission: 403,
  86. ErrBadGateway: 502,
  87. ErrNotImplemented: 501,
  88. ErrInternalError: 500,
  89. ErrResourceNotReady: 500,
  90. ErrPayment: 402,
  91. ErrImageNotFound: 404,
  92. ErrResourceNotFound: 404,
  93. ErrSpecNotFound: 404,
  94. ErrActionNotFound: 404,
  95. ErrTenantNotFound: 404,
  96. ErrUserNotFound: 404,
  97. ErrServerStatus: 400,
  98. ErrInvalidStatus: 400,
  99. ErrInvalidFormat: 400,
  100. ErrInvalidIdpStatus: 400,
  101. ErrInputParameter: 400,
  102. ErrWeakPassword: 400,
  103. ErrMissingParameter: 400,
  104. ErrInsufficientResource: 400,
  105. ErrOutOfResource: 507,
  106. ErrOutOfQuota: 400,
  107. ErrOutOfRange: 400,
  108. ErrOutOfLimit: 400,
  109. ErrNotSufficientPrivilege: 403,
  110. ErrUnsupportedOperation: 406,
  111. ErrNotSupported: 406,
  112. ErrNotEmpty: 406,
  113. ErrBadRequest: 400,
  114. ErrEmptyRequest: 400,
  115. ErrUnauthorized: 401,
  116. ErrInvalidCredential: 401,
  117. ErrForbidden: 403,
  118. ErrNotFound: 404,
  119. ErrNotAcceptable: 406,
  120. ErrDuplicateName: 409,
  121. ErrDuplicateResource: 409,
  122. ErrConflict: 409,
  123. ErrDuplicateId: 409,
  124. ErrResourceBusy: 409,
  125. ErrRequireLicense: 402,
  126. ErrTimeout: 504,
  127. ErrProtectedResource: 403,
  128. ErrNoProject: 403,
  129. ErrTooLarge: 413,
  130. ErrTooManyAttempts: 429,
  131. ErrTooManyRequests: 429,
  132. ErrUserLocked: 423,
  133. ErrUserDisabled: 423,
  134. ErrWrongPassword: 401,
  135. ErrIncorrectUsernameOrPassword: 401,
  136. ErrPolicyDefinition: 409,
  137. ErrInvalidAccessKey: 400,
  138. ErrServiceAbnormal: 499,
  139. }
  140. )
  141. func RegisterErrorHttpCode(err errors.Error, code int) {
  142. if _, ok := httpErrorCode[err]; ok {
  143. panic("Error has been registered: " + string(err))
  144. }
  145. httpErrorCode[err] = code
  146. }