errors.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package exec
  2. import "github.com/pkg/errors"
  3. var (
  4. // ErrRuntimeUnsupported encountered when a task requires a runtime
  5. // unsupported by the executor.
  6. ErrRuntimeUnsupported = errors.New("exec: unsupported runtime")
  7. // ErrTaskPrepared is called if the task is already prepared.
  8. ErrTaskPrepared = errors.New("exec: task already prepared")
  9. // ErrTaskStarted can be returned from any operation that cannot be
  10. // performed because the task has already been started. This does not imply
  11. // that the task is running but rather that it is no longer valid to call
  12. // Start.
  13. ErrTaskStarted = errors.New("exec: task already started")
  14. // ErrTaskUpdateRejected is returned if a task update is rejected by a controller.
  15. ErrTaskUpdateRejected = errors.New("exec: task update rejected")
  16. // ErrControllerClosed returned when a task controller has been closed.
  17. ErrControllerClosed = errors.New("exec: controller closed")
  18. // ErrTaskRetry is returned by Do when an operation failed by should be
  19. // retried. The status should still be reported in this case.
  20. ErrTaskRetry = errors.New("exec: task retry")
  21. // ErrTaskNoop returns when the a subsequent call to Do will not result in
  22. // advancing the task. Callers should avoid calling Do until the task has been updated.
  23. ErrTaskNoop = errors.New("exec: task noop")
  24. )
  25. // ExitCoder is implemented by errors that have an exit code.
  26. type ExitCoder interface {
  27. // ExitCode returns the exit code.
  28. ExitCode() int
  29. }
  30. // Temporary indicates whether or not the error condition is temporary.
  31. //
  32. // If this is encountered in the controller, the failing operation will be
  33. // retried when this returns true. Otherwise, the operation is considered
  34. // fatal.
  35. type Temporary interface {
  36. Temporary() bool
  37. }
  38. // MakeTemporary makes the error temporary.
  39. func MakeTemporary(err error) error {
  40. if IsTemporary(err) {
  41. return err
  42. }
  43. return temporary{err}
  44. }
  45. type temporary struct {
  46. error
  47. }
  48. func (t temporary) Cause() error { return t.error }
  49. func (t temporary) Temporary() bool { return true }
  50. // IsTemporary returns true if the error or a recursive cause returns true for
  51. // temporary.
  52. func IsTemporary(err error) bool {
  53. for err != nil {
  54. if tmp, ok := err.(Temporary); ok && tmp.Temporary() {
  55. return true
  56. }
  57. cause := errors.Cause(err)
  58. if cause == err {
  59. break
  60. }
  61. err = cause
  62. }
  63. return false
  64. }