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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. // __________ .__
  260. // \______ \____________ ____ ____ | |__
  261. // | | _/\_ __ \__ \ / \_/ ___\| | \
  262. // | | \ | | \// __ \| | \ \___| Y \
  263. // |______ / |__| (____ /___| /\___ >___| /
  264. // \/ \/ \/ \/ \/
  265. // ErrBranchDoesNotExist represents an error that branch with such name does not exist.
  266. type ErrBranchDoesNotExist struct {
  267. BranchName string
  268. }
  269. // IsErrBranchDoesNotExist checks if an error is an ErrBranchDoesNotExist.
  270. func IsErrBranchDoesNotExist(err error) bool {
  271. _, ok := err.(ErrBranchDoesNotExist)
  272. return ok
  273. }
  274. func (err ErrBranchDoesNotExist) Error() string {
  275. return fmt.Sprintf("branch does not exist [name: %s]", err.BranchName)
  276. }
  277. func (err ErrBranchDoesNotExist) Unwrap() error {
  278. return util.ErrNotExist
  279. }
  280. // ErrBranchAlreadyExists represents an error that branch with such name already exists.
  281. type ErrBranchAlreadyExists struct {
  282. BranchName string
  283. }
  284. // IsErrBranchAlreadyExists checks if an error is an ErrBranchAlreadyExists.
  285. func IsErrBranchAlreadyExists(err error) bool {
  286. _, ok := err.(ErrBranchAlreadyExists)
  287. return ok
  288. }
  289. func (err ErrBranchAlreadyExists) Error() string {
  290. return fmt.Sprintf("branch already exists [name: %s]", err.BranchName)
  291. }
  292. func (err ErrBranchAlreadyExists) Unwrap() error {
  293. return util.ErrAlreadyExist
  294. }
  295. // ErrBranchNameConflict represents an error that branch name conflicts with other branch.
  296. type ErrBranchNameConflict struct {
  297. BranchName string
  298. }
  299. // IsErrBranchNameConflict checks if an error is an ErrBranchNameConflict.
  300. func IsErrBranchNameConflict(err error) bool {
  301. _, ok := err.(ErrBranchNameConflict)
  302. return ok
  303. }
  304. func (err ErrBranchNameConflict) Error() string {
  305. return fmt.Sprintf("branch conflicts with existing branch [name: %s]", err.BranchName)
  306. }
  307. func (err ErrBranchNameConflict) Unwrap() error {
  308. return util.ErrAlreadyExist
  309. }
  310. // ErrBranchesEqual represents an error that branch name conflicts with other branch.
  311. type ErrBranchesEqual struct {
  312. BaseBranchName string
  313. HeadBranchName string
  314. }
  315. // IsErrBranchesEqual checks if an error is an ErrBranchesEqual.
  316. func IsErrBranchesEqual(err error) bool {
  317. _, ok := err.(ErrBranchesEqual)
  318. return ok
  319. }
  320. func (err ErrBranchesEqual) Error() string {
  321. return fmt.Sprintf("branches are equal [head: %sm base: %s]", err.HeadBranchName, err.BaseBranchName)
  322. }
  323. func (err ErrBranchesEqual) Unwrap() error {
  324. return util.ErrInvalidArgument
  325. }
  326. // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it.
  327. type ErrDisallowedToMerge struct {
  328. Reason string
  329. }
  330. // IsErrDisallowedToMerge checks if an error is an ErrDisallowedToMerge.
  331. func IsErrDisallowedToMerge(err error) bool {
  332. _, ok := err.(ErrDisallowedToMerge)
  333. return ok
  334. }
  335. func (err ErrDisallowedToMerge) Error() string {
  336. return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason)
  337. }
  338. func (err ErrDisallowedToMerge) Unwrap() error {
  339. return util.ErrPermissionDenied
  340. }
  341. // ErrTagAlreadyExists represents an error that tag with such name already exists.
  342. type ErrTagAlreadyExists struct {
  343. TagName string
  344. }
  345. // IsErrTagAlreadyExists checks if an error is an ErrTagAlreadyExists.
  346. func IsErrTagAlreadyExists(err error) bool {
  347. _, ok := err.(ErrTagAlreadyExists)
  348. return ok
  349. }
  350. func (err ErrTagAlreadyExists) Error() string {
  351. return fmt.Sprintf("tag already exists [name: %s]", err.TagName)
  352. }
  353. func (err ErrTagAlreadyExists) Unwrap() error {
  354. return util.ErrAlreadyExist
  355. }
  356. // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error.
  357. type ErrSHADoesNotMatch struct {
  358. Path string
  359. GivenSHA string
  360. CurrentSHA string
  361. }
  362. // IsErrSHADoesNotMatch checks if an error is a ErrSHADoesNotMatch.
  363. func IsErrSHADoesNotMatch(err error) bool {
  364. _, ok := err.(ErrSHADoesNotMatch)
  365. return ok
  366. }
  367. func (err ErrSHADoesNotMatch) Error() string {
  368. return fmt.Sprintf("sha does not match [given: %s, expected: %s]", err.GivenSHA, err.CurrentSHA)
  369. }
  370. // ErrSHANotFound represents a "SHADoesNotMatch" kind of error.
  371. type ErrSHANotFound struct {
  372. SHA string
  373. }
  374. // IsErrSHANotFound checks if an error is a ErrSHANotFound.
  375. func IsErrSHANotFound(err error) bool {
  376. _, ok := err.(ErrSHANotFound)
  377. return ok
  378. }
  379. func (err ErrSHANotFound) Error() string {
  380. return fmt.Sprintf("sha not found [%s]", err.SHA)
  381. }
  382. func (err ErrSHANotFound) Unwrap() error {
  383. return util.ErrNotExist
  384. }
  385. // ErrCommitIDDoesNotMatch represents a "CommitIDDoesNotMatch" kind of error.
  386. type ErrCommitIDDoesNotMatch struct {
  387. GivenCommitID string
  388. CurrentCommitID string
  389. }
  390. // IsErrCommitIDDoesNotMatch checks if an error is a ErrCommitIDDoesNotMatch.
  391. func IsErrCommitIDDoesNotMatch(err error) bool {
  392. _, ok := err.(ErrCommitIDDoesNotMatch)
  393. return ok
  394. }
  395. func (err ErrCommitIDDoesNotMatch) Error() string {
  396. return fmt.Sprintf("file CommitID does not match [given: %s, expected: %s]", err.GivenCommitID, err.CurrentCommitID)
  397. }
  398. // ErrSHAOrCommitIDNotProvided represents a "SHAOrCommitIDNotProvided" kind of error.
  399. type ErrSHAOrCommitIDNotProvided struct{}
  400. // IsErrSHAOrCommitIDNotProvided checks if an error is a ErrSHAOrCommitIDNotProvided.
  401. func IsErrSHAOrCommitIDNotProvided(err error) bool {
  402. _, ok := err.(ErrSHAOrCommitIDNotProvided)
  403. return ok
  404. }
  405. func (err ErrSHAOrCommitIDNotProvided) Error() string {
  406. return "a SHA or commit ID must be proved when updating a file"
  407. }
  408. // ErrInvalidMergeStyle represents an error if merging with disabled merge strategy
  409. type ErrInvalidMergeStyle struct {
  410. ID int64
  411. Style repo_model.MergeStyle
  412. }
  413. // IsErrInvalidMergeStyle checks if an error is a ErrInvalidMergeStyle.
  414. func IsErrInvalidMergeStyle(err error) bool {
  415. _, ok := err.(ErrInvalidMergeStyle)
  416. return ok
  417. }
  418. func (err ErrInvalidMergeStyle) Error() string {
  419. return fmt.Sprintf("merge strategy is not allowed or is invalid [repo_id: %d, strategy: %s]",
  420. err.ID, err.Style)
  421. }
  422. func (err ErrInvalidMergeStyle) Unwrap() error {
  423. return util.ErrInvalidArgument
  424. }
  425. // ErrMergeConflicts represents an error if merging fails with a conflict
  426. type ErrMergeConflicts struct {
  427. Style repo_model.MergeStyle
  428. StdOut string
  429. StdErr string
  430. Err error
  431. }
  432. // IsErrMergeConflicts checks if an error is a ErrMergeConflicts.
  433. func IsErrMergeConflicts(err error) bool {
  434. _, ok := err.(ErrMergeConflicts)
  435. return ok
  436. }
  437. func (err ErrMergeConflicts) Error() string {
  438. return fmt.Sprintf("Merge Conflict Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  439. }
  440. // ErrMergeUnrelatedHistories represents an error if merging fails due to unrelated histories
  441. type ErrMergeUnrelatedHistories struct {
  442. Style repo_model.MergeStyle
  443. StdOut string
  444. StdErr string
  445. Err error
  446. }
  447. // IsErrMergeUnrelatedHistories checks if an error is a ErrMergeUnrelatedHistories.
  448. func IsErrMergeUnrelatedHistories(err error) bool {
  449. _, ok := err.(ErrMergeUnrelatedHistories)
  450. return ok
  451. }
  452. func (err ErrMergeUnrelatedHistories) Error() string {
  453. return fmt.Sprintf("Merge UnrelatedHistories Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  454. }
  455. // ErrRebaseConflicts represents an error if rebase fails with a conflict
  456. type ErrRebaseConflicts struct {
  457. Style repo_model.MergeStyle
  458. CommitSHA string
  459. StdOut string
  460. StdErr string
  461. Err error
  462. }
  463. // IsErrRebaseConflicts checks if an error is a ErrRebaseConflicts.
  464. func IsErrRebaseConflicts(err error) bool {
  465. _, ok := err.(ErrRebaseConflicts)
  466. return ok
  467. }
  468. func (err ErrRebaseConflicts) Error() string {
  469. return fmt.Sprintf("Rebase Error: %v: Whilst Rebasing: %s\n%s\n%s", err.Err, err.CommitSHA, err.StdErr, err.StdOut)
  470. }
  471. // ErrPullRequestHasMerged represents a "PullRequestHasMerged"-error
  472. type ErrPullRequestHasMerged struct {
  473. ID int64
  474. IssueID int64
  475. HeadRepoID int64
  476. BaseRepoID int64
  477. HeadBranch string
  478. BaseBranch string
  479. }
  480. // IsErrPullRequestHasMerged checks if an error is a ErrPullRequestHasMerged.
  481. func IsErrPullRequestHasMerged(err error) bool {
  482. _, ok := err.(ErrPullRequestHasMerged)
  483. return ok
  484. }
  485. // Error does pretty-printing :D
  486. func (err ErrPullRequestHasMerged) Error() string {
  487. 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]",
  488. err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch)
  489. }