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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package models
  5. import (
  6. "fmt"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/util"
  10. )
  11. // ErrUserOwnRepos represents a "UserOwnRepos" kind of error.
  12. type ErrUserOwnRepos struct {
  13. UID int64
  14. }
  15. // IsErrUserOwnRepos checks if an error is a ErrUserOwnRepos.
  16. func IsErrUserOwnRepos(err error) bool {
  17. _, ok := err.(ErrUserOwnRepos)
  18. return ok
  19. }
  20. func (err ErrUserOwnRepos) Error() string {
  21. return fmt.Sprintf("user still has ownership of repositories [uid: %d]", err.UID)
  22. }
  23. // ErrUserHasOrgs represents a "UserHasOrgs" kind of error.
  24. type ErrUserHasOrgs struct {
  25. UID int64
  26. }
  27. // IsErrUserHasOrgs checks if an error is a ErrUserHasOrgs.
  28. func IsErrUserHasOrgs(err error) bool {
  29. _, ok := err.(ErrUserHasOrgs)
  30. return ok
  31. }
  32. func (err ErrUserHasOrgs) Error() string {
  33. return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID)
  34. }
  35. // ErrUserOwnPackages notifies that the user (still) owns the packages.
  36. type ErrUserOwnPackages struct {
  37. UID int64
  38. }
  39. // IsErrUserOwnPackages checks if an error is an ErrUserOwnPackages.
  40. func IsErrUserOwnPackages(err error) bool {
  41. _, ok := err.(ErrUserOwnPackages)
  42. return ok
  43. }
  44. func (err ErrUserOwnPackages) Error() string {
  45. return fmt.Sprintf("user still has ownership of packages [uid: %d]", err.UID)
  46. }
  47. // ErrNoPendingRepoTransfer is an error type for repositories without a pending
  48. // transfer request
  49. type ErrNoPendingRepoTransfer struct {
  50. RepoID int64
  51. }
  52. func (err ErrNoPendingRepoTransfer) Error() string {
  53. return fmt.Sprintf("repository doesn't have a pending transfer [repo_id: %d]", err.RepoID)
  54. }
  55. // IsErrNoPendingTransfer is an error type when a repository has no pending
  56. // transfers
  57. func IsErrNoPendingTransfer(err error) bool {
  58. _, ok := err.(ErrNoPendingRepoTransfer)
  59. return ok
  60. }
  61. func (err ErrNoPendingRepoTransfer) Unwrap() error {
  62. return util.ErrNotExist
  63. }
  64. // ErrRepoTransferInProgress represents the state of a repository that has an
  65. // ongoing transfer
  66. type ErrRepoTransferInProgress struct {
  67. Uname string
  68. Name string
  69. }
  70. // IsErrRepoTransferInProgress checks if an error is a ErrRepoTransferInProgress.
  71. func IsErrRepoTransferInProgress(err error) bool {
  72. _, ok := err.(ErrRepoTransferInProgress)
  73. return ok
  74. }
  75. func (err ErrRepoTransferInProgress) Error() string {
  76. return fmt.Sprintf("repository is already being transferred [uname: %s, name: %s]", err.Uname, err.Name)
  77. }
  78. func (err ErrRepoTransferInProgress) Unwrap() error {
  79. return util.ErrAlreadyExist
  80. }
  81. // ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error.
  82. type ErrInvalidCloneAddr struct {
  83. Host string
  84. IsURLError bool
  85. IsInvalidPath bool
  86. IsProtocolInvalid bool
  87. IsPermissionDenied bool
  88. LocalPath bool
  89. }
  90. // IsErrInvalidCloneAddr checks if an error is a ErrInvalidCloneAddr.
  91. func IsErrInvalidCloneAddr(err error) bool {
  92. _, ok := err.(*ErrInvalidCloneAddr)
  93. return ok
  94. }
  95. func (err *ErrInvalidCloneAddr) Error() string {
  96. if err.IsInvalidPath {
  97. return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided path is invalid", err.Host)
  98. }
  99. if err.IsProtocolInvalid {
  100. return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url protocol is not allowed", err.Host)
  101. }
  102. if err.IsPermissionDenied {
  103. return fmt.Sprintf("migration/cloning from '%s' is not allowed.", err.Host)
  104. }
  105. if err.IsURLError {
  106. return fmt.Sprintf("migration/cloning from '%s' is not allowed: the provided url is invalid", err.Host)
  107. }
  108. return fmt.Sprintf("migration/cloning from '%s' is not allowed", err.Host)
  109. }
  110. func (err *ErrInvalidCloneAddr) Unwrap() error {
  111. return util.ErrInvalidArgument
  112. }
  113. // ErrUpdateTaskNotExist represents a "UpdateTaskNotExist" kind of error.
  114. type ErrUpdateTaskNotExist struct {
  115. UUID string
  116. }
  117. // IsErrUpdateTaskNotExist checks if an error is a ErrUpdateTaskNotExist.
  118. func IsErrUpdateTaskNotExist(err error) bool {
  119. _, ok := err.(ErrUpdateTaskNotExist)
  120. return ok
  121. }
  122. func (err ErrUpdateTaskNotExist) Error() string {
  123. return fmt.Sprintf("update task does not exist [uuid: %s]", err.UUID)
  124. }
  125. func (err ErrUpdateTaskNotExist) Unwrap() error {
  126. return util.ErrNotExist
  127. }
  128. // ErrInvalidTagName represents a "InvalidTagName" kind of error.
  129. type ErrInvalidTagName struct {
  130. TagName string
  131. }
  132. // IsErrInvalidTagName checks if an error is a ErrInvalidTagName.
  133. func IsErrInvalidTagName(err error) bool {
  134. _, ok := err.(ErrInvalidTagName)
  135. return ok
  136. }
  137. func (err ErrInvalidTagName) Error() string {
  138. return fmt.Sprintf("release tag name is not valid [tag_name: %s]", err.TagName)
  139. }
  140. func (err ErrInvalidTagName) Unwrap() error {
  141. return util.ErrInvalidArgument
  142. }
  143. // ErrProtectedTagName represents a "ProtectedTagName" kind of error.
  144. type ErrProtectedTagName struct {
  145. TagName string
  146. }
  147. // IsErrProtectedTagName checks if an error is a ErrProtectedTagName.
  148. func IsErrProtectedTagName(err error) bool {
  149. _, ok := err.(ErrProtectedTagName)
  150. return ok
  151. }
  152. func (err ErrProtectedTagName) Error() string {
  153. return fmt.Sprintf("release tag name is protected [tag_name: %s]", err.TagName)
  154. }
  155. func (err ErrProtectedTagName) Unwrap() error {
  156. return util.ErrPermissionDenied
  157. }
  158. // ErrRepoFileAlreadyExists represents a "RepoFileAlreadyExist" kind of error.
  159. type ErrRepoFileAlreadyExists struct {
  160. Path string
  161. }
  162. // IsErrRepoFileAlreadyExists checks if an error is a ErrRepoFileAlreadyExists.
  163. func IsErrRepoFileAlreadyExists(err error) bool {
  164. _, ok := err.(ErrRepoFileAlreadyExists)
  165. return ok
  166. }
  167. func (err ErrRepoFileAlreadyExists) Error() string {
  168. return fmt.Sprintf("repository file already exists [path: %s]", err.Path)
  169. }
  170. func (err ErrRepoFileAlreadyExists) Unwrap() error {
  171. return util.ErrAlreadyExist
  172. }
  173. // ErrRepoFileDoesNotExist represents a "RepoFileDoesNotExist" kind of error.
  174. type ErrRepoFileDoesNotExist struct {
  175. Path string
  176. Name string
  177. }
  178. // IsErrRepoFileDoesNotExist checks if an error is a ErrRepoDoesNotExist.
  179. func IsErrRepoFileDoesNotExist(err error) bool {
  180. _, ok := err.(ErrRepoFileDoesNotExist)
  181. return ok
  182. }
  183. func (err ErrRepoFileDoesNotExist) Error() string {
  184. return fmt.Sprintf("repository file does not exist [path: %s]", err.Path)
  185. }
  186. func (err ErrRepoFileDoesNotExist) Unwrap() error {
  187. return util.ErrNotExist
  188. }
  189. // ErrFilenameInvalid represents a "FilenameInvalid" kind of error.
  190. type ErrFilenameInvalid struct {
  191. Path string
  192. }
  193. // IsErrFilenameInvalid checks if an error is an ErrFilenameInvalid.
  194. func IsErrFilenameInvalid(err error) bool {
  195. _, ok := err.(ErrFilenameInvalid)
  196. return ok
  197. }
  198. func (err ErrFilenameInvalid) Error() string {
  199. return fmt.Sprintf("path contains a malformed path component [path: %s]", err.Path)
  200. }
  201. func (err ErrFilenameInvalid) Unwrap() error {
  202. return util.ErrInvalidArgument
  203. }
  204. // ErrUserCannotCommit represents "UserCannotCommit" kind of error.
  205. type ErrUserCannotCommit struct {
  206. UserName string
  207. }
  208. // IsErrUserCannotCommit checks if an error is an ErrUserCannotCommit.
  209. func IsErrUserCannotCommit(err error) bool {
  210. _, ok := err.(ErrUserCannotCommit)
  211. return ok
  212. }
  213. func (err ErrUserCannotCommit) Error() string {
  214. return fmt.Sprintf("user cannot commit to repo [user: %s]", err.UserName)
  215. }
  216. func (err ErrUserCannotCommit) Unwrap() error {
  217. return util.ErrPermissionDenied
  218. }
  219. // ErrFilePathInvalid represents a "FilePathInvalid" kind of error.
  220. type ErrFilePathInvalid struct {
  221. Message string
  222. Path string
  223. Name string
  224. Type git.EntryMode
  225. }
  226. // IsErrFilePathInvalid checks if an error is an ErrFilePathInvalid.
  227. func IsErrFilePathInvalid(err error) bool {
  228. _, ok := err.(ErrFilePathInvalid)
  229. return ok
  230. }
  231. func (err ErrFilePathInvalid) Error() string {
  232. if err.Message != "" {
  233. return err.Message
  234. }
  235. return fmt.Sprintf("path is invalid [path: %s]", err.Path)
  236. }
  237. func (err ErrFilePathInvalid) Unwrap() error {
  238. return util.ErrInvalidArgument
  239. }
  240. // ErrFilePathProtected represents a "FilePathProtected" kind of error.
  241. type ErrFilePathProtected struct {
  242. Message string
  243. Path string
  244. }
  245. // IsErrFilePathProtected checks if an error is an ErrFilePathProtected.
  246. func IsErrFilePathProtected(err error) bool {
  247. _, ok := err.(ErrFilePathProtected)
  248. return ok
  249. }
  250. func (err ErrFilePathProtected) Error() string {
  251. if err.Message != "" {
  252. return err.Message
  253. }
  254. return fmt.Sprintf("path is protected and can not be changed [path: %s]", err.Path)
  255. }
  256. func (err ErrFilePathProtected) Unwrap() error {
  257. return util.ErrPermissionDenied
  258. }
  259. // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it.
  260. type ErrDisallowedToMerge struct {
  261. Reason string
  262. }
  263. // IsErrDisallowedToMerge checks if an error is an ErrDisallowedToMerge.
  264. func IsErrDisallowedToMerge(err error) bool {
  265. _, ok := err.(ErrDisallowedToMerge)
  266. return ok
  267. }
  268. func (err ErrDisallowedToMerge) Error() string {
  269. return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason)
  270. }
  271. func (err ErrDisallowedToMerge) Unwrap() error {
  272. return util.ErrPermissionDenied
  273. }
  274. // ErrTagAlreadyExists represents an error that tag with such name already exists.
  275. type ErrTagAlreadyExists struct {
  276. TagName string
  277. }
  278. // IsErrTagAlreadyExists checks if an error is an ErrTagAlreadyExists.
  279. func IsErrTagAlreadyExists(err error) bool {
  280. _, ok := err.(ErrTagAlreadyExists)
  281. return ok
  282. }
  283. func (err ErrTagAlreadyExists) Error() string {
  284. return fmt.Sprintf("tag already exists [name: %s]", err.TagName)
  285. }
  286. func (err ErrTagAlreadyExists) Unwrap() error {
  287. return util.ErrAlreadyExist
  288. }
  289. // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error.
  290. type ErrSHADoesNotMatch struct {
  291. Path string
  292. GivenSHA string
  293. CurrentSHA string
  294. }
  295. // IsErrSHADoesNotMatch checks if an error is a ErrSHADoesNotMatch.
  296. func IsErrSHADoesNotMatch(err error) bool {
  297. _, ok := err.(ErrSHADoesNotMatch)
  298. return ok
  299. }
  300. func (err ErrSHADoesNotMatch) Error() string {
  301. return fmt.Sprintf("sha does not match [given: %s, expected: %s]", err.GivenSHA, err.CurrentSHA)
  302. }
  303. // ErrSHANotFound represents a "SHADoesNotMatch" kind of error.
  304. type ErrSHANotFound struct {
  305. SHA string
  306. }
  307. // IsErrSHANotFound checks if an error is a ErrSHANotFound.
  308. func IsErrSHANotFound(err error) bool {
  309. _, ok := err.(ErrSHANotFound)
  310. return ok
  311. }
  312. func (err ErrSHANotFound) Error() string {
  313. return fmt.Sprintf("sha not found [%s]", err.SHA)
  314. }
  315. func (err ErrSHANotFound) Unwrap() error {
  316. return util.ErrNotExist
  317. }
  318. // ErrCommitIDDoesNotMatch represents a "CommitIDDoesNotMatch" kind of error.
  319. type ErrCommitIDDoesNotMatch struct {
  320. GivenCommitID string
  321. CurrentCommitID string
  322. }
  323. // IsErrCommitIDDoesNotMatch checks if an error is a ErrCommitIDDoesNotMatch.
  324. func IsErrCommitIDDoesNotMatch(err error) bool {
  325. _, ok := err.(ErrCommitIDDoesNotMatch)
  326. return ok
  327. }
  328. func (err ErrCommitIDDoesNotMatch) Error() string {
  329. return fmt.Sprintf("file CommitID does not match [given: %s, expected: %s]", err.GivenCommitID, err.CurrentCommitID)
  330. }
  331. // ErrSHAOrCommitIDNotProvided represents a "SHAOrCommitIDNotProvided" kind of error.
  332. type ErrSHAOrCommitIDNotProvided struct{}
  333. // IsErrSHAOrCommitIDNotProvided checks if an error is a ErrSHAOrCommitIDNotProvided.
  334. func IsErrSHAOrCommitIDNotProvided(err error) bool {
  335. _, ok := err.(ErrSHAOrCommitIDNotProvided)
  336. return ok
  337. }
  338. func (err ErrSHAOrCommitIDNotProvided) Error() string {
  339. return "a SHA or commit ID must be proved when updating a file"
  340. }
  341. // ErrInvalidMergeStyle represents an error if merging with disabled merge strategy
  342. type ErrInvalidMergeStyle struct {
  343. ID int64
  344. Style repo_model.MergeStyle
  345. }
  346. // IsErrInvalidMergeStyle checks if an error is a ErrInvalidMergeStyle.
  347. func IsErrInvalidMergeStyle(err error) bool {
  348. _, ok := err.(ErrInvalidMergeStyle)
  349. return ok
  350. }
  351. func (err ErrInvalidMergeStyle) Error() string {
  352. return fmt.Sprintf("merge strategy is not allowed or is invalid [repo_id: %d, strategy: %s]",
  353. err.ID, err.Style)
  354. }
  355. func (err ErrInvalidMergeStyle) Unwrap() error {
  356. return util.ErrInvalidArgument
  357. }
  358. // ErrMergeConflicts represents an error if merging fails with a conflict
  359. type ErrMergeConflicts struct {
  360. Style repo_model.MergeStyle
  361. StdOut string
  362. StdErr string
  363. Err error
  364. }
  365. // IsErrMergeConflicts checks if an error is a ErrMergeConflicts.
  366. func IsErrMergeConflicts(err error) bool {
  367. _, ok := err.(ErrMergeConflicts)
  368. return ok
  369. }
  370. func (err ErrMergeConflicts) Error() string {
  371. return fmt.Sprintf("Merge Conflict Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  372. }
  373. // ErrMergeUnrelatedHistories represents an error if merging fails due to unrelated histories
  374. type ErrMergeUnrelatedHistories struct {
  375. Style repo_model.MergeStyle
  376. StdOut string
  377. StdErr string
  378. Err error
  379. }
  380. // IsErrMergeUnrelatedHistories checks if an error is a ErrMergeUnrelatedHistories.
  381. func IsErrMergeUnrelatedHistories(err error) bool {
  382. _, ok := err.(ErrMergeUnrelatedHistories)
  383. return ok
  384. }
  385. func (err ErrMergeUnrelatedHistories) Error() string {
  386. return fmt.Sprintf("Merge UnrelatedHistories Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  387. }
  388. // ErrRebaseConflicts represents an error if rebase fails with a conflict
  389. type ErrRebaseConflicts struct {
  390. Style repo_model.MergeStyle
  391. CommitSHA string
  392. StdOut string
  393. StdErr string
  394. Err error
  395. }
  396. // IsErrRebaseConflicts checks if an error is a ErrRebaseConflicts.
  397. func IsErrRebaseConflicts(err error) bool {
  398. _, ok := err.(ErrRebaseConflicts)
  399. return ok
  400. }
  401. func (err ErrRebaseConflicts) Error() string {
  402. return fmt.Sprintf("Rebase Error: %v: Whilst Rebasing: %s\n%s\n%s", err.Err, err.CommitSHA, err.StdErr, err.StdOut)
  403. }
  404. // ErrPullRequestHasMerged represents a "PullRequestHasMerged"-error
  405. type ErrPullRequestHasMerged struct {
  406. ID int64
  407. IssueID int64
  408. HeadRepoID int64
  409. BaseRepoID int64
  410. HeadBranch string
  411. BaseBranch string
  412. }
  413. // IsErrPullRequestHasMerged checks if an error is a ErrPullRequestHasMerged.
  414. func IsErrPullRequestHasMerged(err error) bool {
  415. _, ok := err.(ErrPullRequestHasMerged)
  416. return ok
  417. }
  418. // Error does pretty-printing :D
  419. func (err ErrPullRequestHasMerged) Error() string {
  420. return fmt.Sprintf("pull request has merged [id: %d, issue_id: %d, head_repo_id: %d, base_repo_id: %d, head_branch: %s, base_branch: %s]",
  421. err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch)
  422. }