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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "fmt"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/modules/util"
  9. )
  10. // ErrExecTimeout error when exec timed out
  11. type ErrExecTimeout struct {
  12. Duration time.Duration
  13. }
  14. // IsErrExecTimeout if some error is ErrExecTimeout
  15. func IsErrExecTimeout(err error) bool {
  16. _, ok := err.(ErrExecTimeout)
  17. return ok
  18. }
  19. func (err ErrExecTimeout) Error() string {
  20. return fmt.Sprintf("execution is timeout [duration: %v]", err.Duration)
  21. }
  22. // ErrNotExist commit not exist error
  23. type ErrNotExist struct {
  24. ID string
  25. RelPath string
  26. }
  27. // IsErrNotExist if some error is ErrNotExist
  28. func IsErrNotExist(err error) bool {
  29. _, ok := err.(ErrNotExist)
  30. return ok
  31. }
  32. func (err ErrNotExist) Error() string {
  33. return fmt.Sprintf("object does not exist [id: %s, rel_path: %s]", err.ID, err.RelPath)
  34. }
  35. func (err ErrNotExist) Unwrap() error {
  36. return util.ErrNotExist
  37. }
  38. // ErrBadLink entry.FollowLink error
  39. type ErrBadLink struct {
  40. Name string
  41. Message string
  42. }
  43. func (err ErrBadLink) Error() string {
  44. return fmt.Sprintf("%s: %s", err.Name, err.Message)
  45. }
  46. // IsErrBadLink if some error is ErrBadLink
  47. func IsErrBadLink(err error) bool {
  48. _, ok := err.(ErrBadLink)
  49. return ok
  50. }
  51. // ErrUnsupportedVersion error when required git version not matched
  52. type ErrUnsupportedVersion struct {
  53. Required string
  54. }
  55. // IsErrUnsupportedVersion if some error is ErrUnsupportedVersion
  56. func IsErrUnsupportedVersion(err error) bool {
  57. _, ok := err.(ErrUnsupportedVersion)
  58. return ok
  59. }
  60. func (err ErrUnsupportedVersion) Error() string {
  61. return fmt.Sprintf("Operation requires higher version [required: %s]", err.Required)
  62. }
  63. // ErrBranchNotExist represents a "BranchNotExist" kind of error.
  64. type ErrBranchNotExist struct {
  65. Name string
  66. }
  67. // IsErrBranchNotExist checks if an error is a ErrBranchNotExist.
  68. func IsErrBranchNotExist(err error) bool {
  69. _, ok := err.(ErrBranchNotExist)
  70. return ok
  71. }
  72. func (err ErrBranchNotExist) Error() string {
  73. return fmt.Sprintf("branch does not exist [name: %s]", err.Name)
  74. }
  75. func (err ErrBranchNotExist) Unwrap() error {
  76. return util.ErrNotExist
  77. }
  78. // ErrPushOutOfDate represents an error if merging fails due to unrelated histories
  79. type ErrPushOutOfDate struct {
  80. StdOut string
  81. StdErr string
  82. Err error
  83. }
  84. // IsErrPushOutOfDate checks if an error is a ErrPushOutOfDate.
  85. func IsErrPushOutOfDate(err error) bool {
  86. _, ok := err.(*ErrPushOutOfDate)
  87. return ok
  88. }
  89. func (err *ErrPushOutOfDate) Error() string {
  90. return fmt.Sprintf("PushOutOfDate Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  91. }
  92. // Unwrap unwraps the underlying error
  93. func (err *ErrPushOutOfDate) Unwrap() error {
  94. return fmt.Errorf("%w - %s", err.Err, err.StdErr)
  95. }
  96. // ErrPushRejected represents an error if merging fails due to rejection from a hook
  97. type ErrPushRejected struct {
  98. Message string
  99. StdOut string
  100. StdErr string
  101. Err error
  102. }
  103. // IsErrPushRejected checks if an error is a ErrPushRejected.
  104. func IsErrPushRejected(err error) bool {
  105. _, ok := err.(*ErrPushRejected)
  106. return ok
  107. }
  108. func (err *ErrPushRejected) Error() string {
  109. return fmt.Sprintf("PushRejected Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  110. }
  111. // Unwrap unwraps the underlying error
  112. func (err *ErrPushRejected) Unwrap() error {
  113. return fmt.Errorf("%w - %s", err.Err, err.StdErr)
  114. }
  115. // GenerateMessage generates the remote message from the stderr
  116. func (err *ErrPushRejected) GenerateMessage() {
  117. messageBuilder := &strings.Builder{}
  118. i := strings.Index(err.StdErr, "remote: ")
  119. if i < 0 {
  120. err.Message = ""
  121. return
  122. }
  123. for {
  124. if len(err.StdErr) <= i+8 {
  125. break
  126. }
  127. if err.StdErr[i:i+8] != "remote: " {
  128. break
  129. }
  130. i += 8
  131. nl := strings.IndexByte(err.StdErr[i:], '\n')
  132. if nl >= 0 {
  133. messageBuilder.WriteString(err.StdErr[i : i+nl+1])
  134. i = i + nl + 1
  135. } else {
  136. messageBuilder.WriteString(err.StdErr[i:])
  137. i = len(err.StdErr)
  138. }
  139. }
  140. err.Message = strings.TrimSpace(messageBuilder.String())
  141. }
  142. // ErrMoreThanOne represents an error if pull request fails when there are more than one sources (branch, tag) with the same name
  143. type ErrMoreThanOne struct {
  144. StdOut string
  145. StdErr string
  146. Err error
  147. }
  148. // IsErrMoreThanOne checks if an error is a ErrMoreThanOne
  149. func IsErrMoreThanOne(err error) bool {
  150. _, ok := err.(*ErrMoreThanOne)
  151. return ok
  152. }
  153. func (err *ErrMoreThanOne) Error() string {
  154. return fmt.Sprintf("ErrMoreThanOne Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  155. }