gitea源码

error.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/modules/util"
  10. )
  11. // ErrNotExist commit not exist error
  12. type ErrNotExist struct {
  13. ID string
  14. RelPath string
  15. }
  16. // IsErrNotExist if some error is ErrNotExist
  17. func IsErrNotExist(err error) bool {
  18. _, ok := err.(ErrNotExist)
  19. return ok
  20. }
  21. func (err ErrNotExist) Error() string {
  22. return fmt.Sprintf("object does not exist [id: %s, rel_path: %s]", err.ID, err.RelPath)
  23. }
  24. func (err ErrNotExist) Unwrap() error {
  25. return util.ErrNotExist
  26. }
  27. // ErrBranchNotExist represents a "BranchNotExist" kind of error.
  28. type ErrBranchNotExist struct {
  29. Name string
  30. }
  31. // IsErrBranchNotExist checks if an error is a ErrBranchNotExist.
  32. func IsErrBranchNotExist(err error) bool {
  33. _, ok := err.(ErrBranchNotExist)
  34. return ok
  35. }
  36. func (err ErrBranchNotExist) Error() string {
  37. return fmt.Sprintf("branch does not exist [name: %s]", err.Name)
  38. }
  39. func (err ErrBranchNotExist) Unwrap() error {
  40. return util.ErrNotExist
  41. }
  42. // ErrPushOutOfDate represents an error if merging fails due to the base branch being updated
  43. type ErrPushOutOfDate struct {
  44. StdOut string
  45. StdErr string
  46. Err error
  47. }
  48. // IsErrPushOutOfDate checks if an error is a ErrPushOutOfDate.
  49. func IsErrPushOutOfDate(err error) bool {
  50. _, ok := err.(*ErrPushOutOfDate)
  51. return ok
  52. }
  53. func (err *ErrPushOutOfDate) Error() string {
  54. return fmt.Sprintf("PushOutOfDate Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  55. }
  56. // Unwrap unwraps the underlying error
  57. func (err *ErrPushOutOfDate) Unwrap() error {
  58. return fmt.Errorf("%w - %s", err.Err, err.StdErr)
  59. }
  60. // ErrPushRejected represents an error if merging fails due to rejection from a hook
  61. type ErrPushRejected struct {
  62. Message string
  63. StdOut string
  64. StdErr string
  65. Err error
  66. }
  67. // IsErrPushRejected checks if an error is a ErrPushRejected.
  68. func IsErrPushRejected(err error) bool {
  69. _, ok := err.(*ErrPushRejected)
  70. return ok
  71. }
  72. func (err *ErrPushRejected) Error() string {
  73. return fmt.Sprintf("PushRejected Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  74. }
  75. // Unwrap unwraps the underlying error
  76. func (err *ErrPushRejected) Unwrap() error {
  77. return fmt.Errorf("%w - %s", err.Err, err.StdErr)
  78. }
  79. // GenerateMessage generates the remote message from the stderr
  80. func (err *ErrPushRejected) GenerateMessage() {
  81. messageBuilder := &strings.Builder{}
  82. i := strings.Index(err.StdErr, "remote: ")
  83. if i < 0 {
  84. err.Message = ""
  85. return
  86. }
  87. for {
  88. if len(err.StdErr) <= i+8 {
  89. break
  90. }
  91. if err.StdErr[i:i+8] != "remote: " {
  92. break
  93. }
  94. i += 8
  95. nl := strings.IndexByte(err.StdErr[i:], '\n')
  96. if nl >= 0 {
  97. messageBuilder.WriteString(err.StdErr[i : i+nl+1])
  98. i = i + nl + 1
  99. } else {
  100. messageBuilder.WriteString(err.StdErr[i:])
  101. i = len(err.StdErr)
  102. }
  103. }
  104. err.Message = strings.TrimSpace(messageBuilder.String())
  105. }
  106. // ErrMoreThanOne represents an error if pull request fails when there are more than one sources (branch, tag) with the same name
  107. type ErrMoreThanOne struct {
  108. StdOut string
  109. StdErr string
  110. Err error
  111. }
  112. // IsErrMoreThanOne checks if an error is a ErrMoreThanOne
  113. func IsErrMoreThanOne(err error) bool {
  114. _, ok := err.(*ErrMoreThanOne)
  115. return ok
  116. }
  117. func (err *ErrMoreThanOne) Error() string {
  118. return fmt.Sprintf("ErrMoreThanOne Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
  119. }
  120. func IsErrCanceledOrKilled(err error) bool {
  121. // When "cancel()" a git command's context, the returned error of "Run()" could be one of them:
  122. // - context.Canceled
  123. // - *exec.ExitError: "signal: killed"
  124. return err != nil && (errors.Is(err, context.Canceled) || err.Error() == "signal: killed")
  125. }