errors_task.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package errors
  2. import (
  3. "fmt"
  4. "strings"
  5. "mvdan.cc/sh/v3/interp"
  6. )
  7. // TaskNotFoundError is returned when the specified task is not found in the
  8. // Taskfile.
  9. type TaskNotFoundError struct {
  10. TaskName string
  11. DidYouMean string
  12. }
  13. func (err *TaskNotFoundError) Error() string {
  14. if err.DidYouMean != "" {
  15. return fmt.Sprintf(
  16. `task: Task %q does not exist. Did you mean %q?`,
  17. err.TaskName,
  18. err.DidYouMean,
  19. )
  20. }
  21. return fmt.Sprintf(`task: Task %q does not exist`, err.TaskName)
  22. }
  23. func (err *TaskNotFoundError) Code() int {
  24. return CodeTaskNotFound
  25. }
  26. // TaskRunError is returned when a command in a task returns a non-zero exit
  27. // code.
  28. type TaskRunError struct {
  29. TaskName string
  30. Err error
  31. }
  32. func (err *TaskRunError) Error() string {
  33. return fmt.Sprintf(`task: Failed to run task %q: %v`, err.TaskName, err.Err)
  34. }
  35. func (err *TaskRunError) Code() int {
  36. return CodeTaskRunError
  37. }
  38. func (err *TaskRunError) TaskExitCode() int {
  39. if c, ok := interp.IsExitStatus(err.Err); ok {
  40. return int(c)
  41. }
  42. return err.Code()
  43. }
  44. // TaskInternalError when the user attempts to invoke a task that is internal.
  45. type TaskInternalError struct {
  46. TaskName string
  47. }
  48. func (err *TaskInternalError) Error() string {
  49. return fmt.Sprintf(`task: Task "%s" is internal`, err.TaskName)
  50. }
  51. func (err *TaskInternalError) Code() int {
  52. return CodeTaskInternal
  53. }
  54. // TaskNameConflictError is returned when multiple tasks with a matching name or
  55. // alias are found.
  56. type TaskNameConflictError struct {
  57. Call string
  58. TaskNames []string
  59. }
  60. func (err *TaskNameConflictError) Error() string {
  61. return fmt.Sprintf(`task: Found multiple tasks (%s) that match %q`, strings.Join(err.TaskNames, ", "), err.Call)
  62. }
  63. func (err *TaskNameConflictError) Code() int {
  64. return CodeTaskNameConflict
  65. }
  66. type TaskNameFlattenConflictError struct {
  67. TaskName string
  68. Include string
  69. }
  70. func (err *TaskNameFlattenConflictError) Error() string {
  71. return fmt.Sprintf(`task: Found multiple tasks (%s) included by "%s""`, err.TaskName, err.Include)
  72. }
  73. func (err *TaskNameFlattenConflictError) Code() int {
  74. return CodeTaskNameConflict
  75. }
  76. // TaskCalledTooManyTimesError is returned when the maximum task call limit is
  77. // exceeded. This is to prevent infinite loops and cyclic dependencies.
  78. type TaskCalledTooManyTimesError struct {
  79. TaskName string
  80. MaximumTaskCall int
  81. }
  82. func (err *TaskCalledTooManyTimesError) Error() string {
  83. return fmt.Sprintf(
  84. `task: Maximum task call exceeded (%d) for task %q: probably an cyclic dep or infinite loop`,
  85. err.MaximumTaskCall,
  86. err.TaskName,
  87. )
  88. }
  89. func (err *TaskCalledTooManyTimesError) Code() int {
  90. return CodeTaskCalledTooManyTimes
  91. }
  92. // TaskCancelledByUserError is returned when the user does not accept an optional prompt to continue.
  93. type TaskCancelledByUserError struct {
  94. TaskName string
  95. }
  96. func (err *TaskCancelledByUserError) Error() string {
  97. return fmt.Sprintf(`task: Task %q cancelled by user`, err.TaskName)
  98. }
  99. func (err *TaskCancelledByUserError) Code() int {
  100. return CodeTaskCancelled
  101. }
  102. // TaskCancelledNoTerminalError is returned when trying to run a task with a prompt in a non-terminal environment.
  103. type TaskCancelledNoTerminalError struct {
  104. TaskName string
  105. }
  106. func (err *TaskCancelledNoTerminalError) Error() string {
  107. return fmt.Sprintf(
  108. `task: Task %q cancelled because it has a prompt and the environment is not a terminal. Use --yes (-y) to run anyway.`,
  109. err.TaskName,
  110. )
  111. }
  112. func (err *TaskCancelledNoTerminalError) Code() int {
  113. return CodeTaskCancelled
  114. }
  115. // TaskMissingRequiredVars is returned when a task is missing required variables.
  116. type TaskMissingRequiredVars struct {
  117. TaskName string
  118. MissingVars []string
  119. }
  120. func (err *TaskMissingRequiredVars) Error() string {
  121. return fmt.Sprintf(
  122. `task: Task %q cancelled because it is missing required variables: %s`,
  123. err.TaskName,
  124. strings.Join(err.MissingVars, ", "),
  125. )
  126. }
  127. func (err *TaskMissingRequiredVars) Code() int {
  128. return CodeTaskMissingRequiredVars
  129. }