errors.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package vcs
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. // The vcs package provides ways to work with errors that hide the underlying
  7. // implementation details but make them accessible if needed. For basic errors
  8. // that do not have underlying implementation specific details or the underlying
  9. // details are not necessary there are errors for comparison.
  10. //
  11. // For example:
  12. //
  13. // ci, err := repo.CommitInfo("123")
  14. // if err == vcs.ErrRevisionUnavailable {
  15. // // The commit id was not available in the VCS.
  16. // }
  17. //
  18. // There are other times where getting the details are more useful. For example,
  19. // if you're performing a repo.Get() and an error occurs. In general you'll want
  20. // to consistently know it failed. But, you may want to know the underlying
  21. // details (opt-in) to them. For those cases there is a different form of error
  22. // handling.
  23. //
  24. // For example:
  25. //
  26. // err := repo.Get()
  27. // if err != nil {
  28. // // A RemoteError was returned. This has access to the output of the
  29. // // vcs command, original error, and has a consistent cross vcs message.
  30. // }
  31. //
  32. // The errors returned here can be used in type switches to detect the underlying
  33. // error. For example:
  34. //
  35. // switch err.(type) {
  36. // case *vcs.RemoteError:
  37. // // This an error connecting to a remote system.
  38. // }
  39. //
  40. // For more information on using type switches to detect error types you can
  41. // read the Go wiki at https://github.com/golang/go/wiki/Errors
  42. var (
  43. // ErrWrongVCS is returned when an action is tried on the wrong VCS.
  44. ErrWrongVCS = errors.New("Wrong VCS detected")
  45. // ErrCannotDetectVCS is returned when VCS cannot be detected from URI string.
  46. ErrCannotDetectVCS = errors.New("Cannot detect VCS")
  47. // ErrWrongRemote occurs when the passed in remote does not match the VCS
  48. // configured endpoint.
  49. ErrWrongRemote = errors.New("The Remote does not match the VCS endpoint")
  50. // ErrRevisionUnavailable happens when commit revision information is
  51. // unavailable.
  52. ErrRevisionUnavailable = errors.New("Revision unavailable")
  53. )
  54. // RemoteError is returned when an operation fails against a remote repo
  55. type RemoteError struct {
  56. vcsError
  57. }
  58. // NewRemoteError constructs a RemoteError
  59. func NewRemoteError(msg string, err error, out string) error {
  60. e := &RemoteError{}
  61. e.s = msg
  62. e.e = err
  63. e.o = out
  64. return e
  65. }
  66. // LocalError is returned when a local operation has an error
  67. type LocalError struct {
  68. vcsError
  69. }
  70. // NewLocalError constructs a LocalError
  71. func NewLocalError(msg string, err error, out string) error {
  72. e := &LocalError{}
  73. e.s = msg
  74. e.e = err
  75. e.o = out
  76. return e
  77. }
  78. type vcsError struct {
  79. s string
  80. e error // The original error
  81. o string // The output from executing the command
  82. }
  83. // Error implements the Error interface
  84. func (e *vcsError) Error() string {
  85. if e.e == nil {
  86. return e.s
  87. }
  88. return fmt.Sprintf("%s: %v", e.s, e.e)
  89. }
  90. // Original retrieves the underlying implementation specific error.
  91. func (e *vcsError) Original() error {
  92. return e.e
  93. }
  94. // Out retrieves the output of the original command that was run.
  95. func (e *vcsError) Out() string {
  96. return e.o
  97. }