errors_taskfile.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package errors
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/Masterminds/semver/v3"
  7. )
  8. // TaskfileNotFoundError is returned when no appropriate Taskfile is found when
  9. // searching the filesystem.
  10. type TaskfileNotFoundError struct {
  11. URI string
  12. Walk bool
  13. }
  14. func (err TaskfileNotFoundError) Error() string {
  15. var walkText string
  16. if err.Walk {
  17. walkText = " (or any of the parent directories)"
  18. }
  19. return fmt.Sprintf(`task: No Taskfile found at %q%s`, err.URI, walkText)
  20. }
  21. func (err TaskfileNotFoundError) Code() int {
  22. return CodeTaskfileNotFound
  23. }
  24. // TaskfileAlreadyExistsError is returned on creating a Taskfile if one already
  25. // exists.
  26. type TaskfileAlreadyExistsError struct{}
  27. func (err TaskfileAlreadyExistsError) Error() string {
  28. return "task: A Taskfile already exists"
  29. }
  30. func (err TaskfileAlreadyExistsError) Code() int {
  31. return CodeTaskfileAlreadyExists
  32. }
  33. // TaskfileInvalidError is returned when the Taskfile contains syntax errors or
  34. // cannot be parsed for some reason.
  35. type TaskfileInvalidError struct {
  36. URI string
  37. Err error
  38. }
  39. func (err TaskfileInvalidError) Error() string {
  40. return fmt.Sprintf("task: Failed to parse %s:\n%v", err.URI, err.Err)
  41. }
  42. func (err TaskfileInvalidError) Code() int {
  43. return CodeTaskfileInvalid
  44. }
  45. // TaskfileFetchFailedError is returned when no appropriate Taskfile is found when
  46. // searching the filesystem.
  47. type TaskfileFetchFailedError struct {
  48. URI string
  49. HTTPStatusCode int
  50. }
  51. func (err TaskfileFetchFailedError) Error() string {
  52. var statusText string
  53. if err.HTTPStatusCode != 0 {
  54. statusText = fmt.Sprintf(" with status code %d (%s)", err.HTTPStatusCode, http.StatusText(err.HTTPStatusCode))
  55. }
  56. return fmt.Sprintf(`task: Download of %q failed%s`, err.URI, statusText)
  57. }
  58. func (err TaskfileFetchFailedError) Code() int {
  59. return CodeTaskfileFetchFailed
  60. }
  61. // TaskfileNotTrustedError is returned when the user does not accept the trust
  62. // prompt when downloading a remote Taskfile.
  63. type TaskfileNotTrustedError struct {
  64. URI string
  65. }
  66. func (err *TaskfileNotTrustedError) Error() string {
  67. return fmt.Sprintf(
  68. `task: Taskfile %q not trusted by user`,
  69. err.URI,
  70. )
  71. }
  72. func (err *TaskfileNotTrustedError) Code() int {
  73. return CodeTaskfileNotTrusted
  74. }
  75. // TaskfileNotSecureError is returned when the user attempts to download a
  76. // remote Taskfile over an insecure connection.
  77. type TaskfileNotSecureError struct {
  78. URI string
  79. }
  80. func (err *TaskfileNotSecureError) Error() string {
  81. return fmt.Sprintf(
  82. `task: Taskfile %q cannot be downloaded over an insecure connection. You can override this by using the --insecure flag`,
  83. err.URI,
  84. )
  85. }
  86. func (err *TaskfileNotSecureError) Code() int {
  87. return CodeTaskfileNotSecure
  88. }
  89. // TaskfileCacheNotFoundError is returned when the user attempts to use an offline
  90. // (cached) Taskfile but the files does not exist in the cache.
  91. type TaskfileCacheNotFoundError struct {
  92. URI string
  93. }
  94. func (err *TaskfileCacheNotFoundError) Error() string {
  95. return fmt.Sprintf(
  96. `task: Taskfile %q was not found in the cache. Remove the --offline flag to use a remote copy or download it using the --download flag`,
  97. err.URI,
  98. )
  99. }
  100. func (err *TaskfileCacheNotFoundError) Code() int {
  101. return CodeTaskfileCacheNotFound
  102. }
  103. // TaskfileVersionCheckError is returned when the user attempts to run a
  104. // Taskfile that does not contain a Taskfile schema version key or if they try
  105. // to use a feature that is not supported by the schema version.
  106. type TaskfileVersionCheckError struct {
  107. URI string
  108. SchemaVersion *semver.Version
  109. Message string
  110. }
  111. func (err *TaskfileVersionCheckError) Error() string {
  112. if err.SchemaVersion == nil {
  113. return fmt.Sprintf(
  114. `task: Missing schema version in Taskfile %q`,
  115. err.URI,
  116. )
  117. }
  118. return fmt.Sprintf(
  119. "task: Invalid schema version in Taskfile %q:\nSchema version (%s) %s",
  120. err.URI,
  121. err.SchemaVersion.String(),
  122. err.Message,
  123. )
  124. }
  125. func (err *TaskfileVersionCheckError) Code() int {
  126. return CodeTaskfileVersionCheckError
  127. }
  128. // TaskfileNetworkTimeoutError is returned when the user attempts to use a remote
  129. // Taskfile but a network connection could not be established within the timeout.
  130. type TaskfileNetworkTimeoutError struct {
  131. URI string
  132. Timeout time.Duration
  133. CheckedCache bool
  134. }
  135. func (err *TaskfileNetworkTimeoutError) Error() string {
  136. var cacheText string
  137. if err.CheckedCache {
  138. cacheText = " and no offline copy was found in the cache"
  139. }
  140. return fmt.Sprintf(
  141. `task: Network connection timed out after %s while attempting to download Taskfile %q%s`,
  142. err.Timeout, err.URI, cacheText,
  143. )
  144. }
  145. func (err *TaskfileNetworkTimeoutError) Code() int {
  146. return CodeTaskfileNetworkTimeout
  147. }
  148. // TaskfileCycleError is returned when we detect that a Taskfile includes a
  149. // set of Taskfiles that include each other in a cycle.
  150. type TaskfileCycleError struct {
  151. Source string
  152. Destination string
  153. }
  154. func (err TaskfileCycleError) Error() string {
  155. return fmt.Sprintf("task: include cycle detected between %s <--> %s",
  156. err.Source,
  157. err.Destination,
  158. )
  159. }
  160. func (err TaskfileCycleError) Code() int {
  161. return CodeTaskfileCycle
  162. }