You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error.go 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package asymkey
  4. import (
  5. "fmt"
  6. "code.gitea.io/gitea/modules/util"
  7. )
  8. // ErrKeyUnableVerify represents a "KeyUnableVerify" kind of error.
  9. type ErrKeyUnableVerify struct {
  10. Result string
  11. }
  12. // IsErrKeyUnableVerify checks if an error is a ErrKeyUnableVerify.
  13. func IsErrKeyUnableVerify(err error) bool {
  14. _, ok := err.(ErrKeyUnableVerify)
  15. return ok
  16. }
  17. func (err ErrKeyUnableVerify) Error() string {
  18. return fmt.Sprintf("Unable to verify key content [result: %s]", err.Result)
  19. }
  20. // ErrKeyNotExist represents a "KeyNotExist" kind of error.
  21. type ErrKeyNotExist struct {
  22. ID int64
  23. }
  24. // IsErrKeyNotExist checks if an error is a ErrKeyNotExist.
  25. func IsErrKeyNotExist(err error) bool {
  26. _, ok := err.(ErrKeyNotExist)
  27. return ok
  28. }
  29. func (err ErrKeyNotExist) Error() string {
  30. return fmt.Sprintf("public key does not exist [id: %d]", err.ID)
  31. }
  32. func (err ErrKeyNotExist) Unwrap() error {
  33. return util.ErrNotExist
  34. }
  35. // ErrKeyAlreadyExist represents a "KeyAlreadyExist" kind of error.
  36. type ErrKeyAlreadyExist struct {
  37. OwnerID int64
  38. Fingerprint string
  39. Content string
  40. }
  41. // IsErrKeyAlreadyExist checks if an error is a ErrKeyAlreadyExist.
  42. func IsErrKeyAlreadyExist(err error) bool {
  43. _, ok := err.(ErrKeyAlreadyExist)
  44. return ok
  45. }
  46. func (err ErrKeyAlreadyExist) Error() string {
  47. return fmt.Sprintf("public key already exists [owner_id: %d, finger_print: %s, content: %s]",
  48. err.OwnerID, err.Fingerprint, err.Content)
  49. }
  50. func (err ErrKeyAlreadyExist) Unwrap() error {
  51. return util.ErrAlreadyExist
  52. }
  53. // ErrKeyNameAlreadyUsed represents a "KeyNameAlreadyUsed" kind of error.
  54. type ErrKeyNameAlreadyUsed struct {
  55. OwnerID int64
  56. Name string
  57. }
  58. // IsErrKeyNameAlreadyUsed checks if an error is a ErrKeyNameAlreadyUsed.
  59. func IsErrKeyNameAlreadyUsed(err error) bool {
  60. _, ok := err.(ErrKeyNameAlreadyUsed)
  61. return ok
  62. }
  63. func (err ErrKeyNameAlreadyUsed) Error() string {
  64. return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name)
  65. }
  66. func (err ErrKeyNameAlreadyUsed) Unwrap() error {
  67. return util.ErrAlreadyExist
  68. }
  69. // ErrGPGNoEmailFound represents a "ErrGPGNoEmailFound" kind of error.
  70. type ErrGPGNoEmailFound struct {
  71. FailedEmails []string
  72. ID string
  73. }
  74. // IsErrGPGNoEmailFound checks if an error is a ErrGPGNoEmailFound.
  75. func IsErrGPGNoEmailFound(err error) bool {
  76. _, ok := err.(ErrGPGNoEmailFound)
  77. return ok
  78. }
  79. func (err ErrGPGNoEmailFound) Error() string {
  80. return fmt.Sprintf("none of the emails attached to the GPG key could be found: %v", err.FailedEmails)
  81. }
  82. // ErrGPGInvalidTokenSignature represents a "ErrGPGInvalidTokenSignature" kind of error.
  83. type ErrGPGInvalidTokenSignature struct {
  84. Wrapped error
  85. ID string
  86. }
  87. // IsErrGPGInvalidTokenSignature checks if an error is a ErrGPGInvalidTokenSignature.
  88. func IsErrGPGInvalidTokenSignature(err error) bool {
  89. _, ok := err.(ErrGPGInvalidTokenSignature)
  90. return ok
  91. }
  92. func (err ErrGPGInvalidTokenSignature) Error() string {
  93. return "the provided signature does not sign the token with the provided key"
  94. }
  95. // ErrGPGKeyParsing represents a "ErrGPGKeyParsing" kind of error.
  96. type ErrGPGKeyParsing struct {
  97. ParseError error
  98. }
  99. // IsErrGPGKeyParsing checks if an error is a ErrGPGKeyParsing.
  100. func IsErrGPGKeyParsing(err error) bool {
  101. _, ok := err.(ErrGPGKeyParsing)
  102. return ok
  103. }
  104. func (err ErrGPGKeyParsing) Error() string {
  105. return fmt.Sprintf("failed to parse gpg key %s", err.ParseError.Error())
  106. }
  107. // ErrGPGKeyNotExist represents a "GPGKeyNotExist" kind of error.
  108. type ErrGPGKeyNotExist struct {
  109. ID int64
  110. }
  111. // IsErrGPGKeyNotExist checks if an error is a ErrGPGKeyNotExist.
  112. func IsErrGPGKeyNotExist(err error) bool {
  113. _, ok := err.(ErrGPGKeyNotExist)
  114. return ok
  115. }
  116. func (err ErrGPGKeyNotExist) Error() string {
  117. return fmt.Sprintf("public gpg key does not exist [id: %d]", err.ID)
  118. }
  119. func (err ErrGPGKeyNotExist) Unwrap() error {
  120. return util.ErrNotExist
  121. }
  122. // ErrGPGKeyImportNotExist represents a "GPGKeyImportNotExist" kind of error.
  123. type ErrGPGKeyImportNotExist struct {
  124. ID string
  125. }
  126. // IsErrGPGKeyImportNotExist checks if an error is a ErrGPGKeyImportNotExist.
  127. func IsErrGPGKeyImportNotExist(err error) bool {
  128. _, ok := err.(ErrGPGKeyImportNotExist)
  129. return ok
  130. }
  131. func (err ErrGPGKeyImportNotExist) Error() string {
  132. return fmt.Sprintf("public gpg key import does not exist [id: %s]", err.ID)
  133. }
  134. func (err ErrGPGKeyImportNotExist) Unwrap() error {
  135. return util.ErrNotExist
  136. }
  137. // ErrGPGKeyIDAlreadyUsed represents a "GPGKeyIDAlreadyUsed" kind of error.
  138. type ErrGPGKeyIDAlreadyUsed struct {
  139. KeyID string
  140. }
  141. // IsErrGPGKeyIDAlreadyUsed checks if an error is a ErrKeyNameAlreadyUsed.
  142. func IsErrGPGKeyIDAlreadyUsed(err error) bool {
  143. _, ok := err.(ErrGPGKeyIDAlreadyUsed)
  144. return ok
  145. }
  146. func (err ErrGPGKeyIDAlreadyUsed) Error() string {
  147. return fmt.Sprintf("public key already exists [key_id: %s]", err.KeyID)
  148. }
  149. func (err ErrGPGKeyIDAlreadyUsed) Unwrap() error {
  150. return util.ErrAlreadyExist
  151. }
  152. // ErrGPGKeyAccessDenied represents a "GPGKeyAccessDenied" kind of Error.
  153. type ErrGPGKeyAccessDenied struct {
  154. UserID int64
  155. KeyID int64
  156. }
  157. // IsErrGPGKeyAccessDenied checks if an error is a ErrGPGKeyAccessDenied.
  158. func IsErrGPGKeyAccessDenied(err error) bool {
  159. _, ok := err.(ErrGPGKeyAccessDenied)
  160. return ok
  161. }
  162. // Error pretty-prints an error of type ErrGPGKeyAccessDenied.
  163. func (err ErrGPGKeyAccessDenied) Error() string {
  164. return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d]",
  165. err.UserID, err.KeyID)
  166. }
  167. func (err ErrGPGKeyAccessDenied) Unwrap() error {
  168. return util.ErrPermissionDenied
  169. }
  170. // ErrKeyAccessDenied represents a "KeyAccessDenied" kind of error.
  171. type ErrKeyAccessDenied struct {
  172. UserID int64
  173. KeyID int64
  174. Note string
  175. }
  176. // IsErrKeyAccessDenied checks if an error is a ErrKeyAccessDenied.
  177. func IsErrKeyAccessDenied(err error) bool {
  178. _, ok := err.(ErrKeyAccessDenied)
  179. return ok
  180. }
  181. func (err ErrKeyAccessDenied) Error() string {
  182. return fmt.Sprintf("user does not have access to the key [user_id: %d, key_id: %d, note: %s]",
  183. err.UserID, err.KeyID, err.Note)
  184. }
  185. func (err ErrKeyAccessDenied) Unwrap() error {
  186. return util.ErrPermissionDenied
  187. }
  188. // ErrDeployKeyNotExist represents a "DeployKeyNotExist" kind of error.
  189. type ErrDeployKeyNotExist struct {
  190. ID int64
  191. KeyID int64
  192. RepoID int64
  193. }
  194. // IsErrDeployKeyNotExist checks if an error is a ErrDeployKeyNotExist.
  195. func IsErrDeployKeyNotExist(err error) bool {
  196. _, ok := err.(ErrDeployKeyNotExist)
  197. return ok
  198. }
  199. func (err ErrDeployKeyNotExist) Error() string {
  200. return fmt.Sprintf("Deploy key does not exist [id: %d, key_id: %d, repo_id: %d]", err.ID, err.KeyID, err.RepoID)
  201. }
  202. func (err ErrDeployKeyNotExist) Unwrap() error {
  203. return util.ErrNotExist
  204. }
  205. // ErrDeployKeyAlreadyExist represents a "DeployKeyAlreadyExist" kind of error.
  206. type ErrDeployKeyAlreadyExist struct {
  207. KeyID int64
  208. RepoID int64
  209. }
  210. // IsErrDeployKeyAlreadyExist checks if an error is a ErrDeployKeyAlreadyExist.
  211. func IsErrDeployKeyAlreadyExist(err error) bool {
  212. _, ok := err.(ErrDeployKeyAlreadyExist)
  213. return ok
  214. }
  215. func (err ErrDeployKeyAlreadyExist) Error() string {
  216. return fmt.Sprintf("public key already exists [key_id: %d, repo_id: %d]", err.KeyID, err.RepoID)
  217. }
  218. func (err ErrDeployKeyAlreadyExist) Unwrap() error {
  219. return util.ErrAlreadyExist
  220. }
  221. // ErrDeployKeyNameAlreadyUsed represents a "DeployKeyNameAlreadyUsed" kind of error.
  222. type ErrDeployKeyNameAlreadyUsed struct {
  223. RepoID int64
  224. Name string
  225. }
  226. // IsErrDeployKeyNameAlreadyUsed checks if an error is a ErrDeployKeyNameAlreadyUsed.
  227. func IsErrDeployKeyNameAlreadyUsed(err error) bool {
  228. _, ok := err.(ErrDeployKeyNameAlreadyUsed)
  229. return ok
  230. }
  231. func (err ErrDeployKeyNameAlreadyUsed) Error() string {
  232. return fmt.Sprintf("public key with name already exists [repo_id: %d, name: %s]", err.RepoID, err.Name)
  233. }
  234. func (err ErrDeployKeyNameAlreadyUsed) Unwrap() error {
  235. return util.ErrNotExist
  236. }
  237. // ErrSSHInvalidTokenSignature represents a "ErrSSHInvalidTokenSignature" kind of error.
  238. type ErrSSHInvalidTokenSignature struct {
  239. Wrapped error
  240. Fingerprint string
  241. }
  242. // IsErrSSHInvalidTokenSignature checks if an error is a ErrSSHInvalidTokenSignature.
  243. func IsErrSSHInvalidTokenSignature(err error) bool {
  244. _, ok := err.(ErrSSHInvalidTokenSignature)
  245. return ok
  246. }
  247. func (err ErrSSHInvalidTokenSignature) Error() string {
  248. return "the provided signature does not sign the token with the provided key"
  249. }
  250. func (err ErrSSHInvalidTokenSignature) Unwrap() error {
  251. return util.ErrInvalidArgument
  252. }