juju_adaptor.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package errors
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // ==================== juju adaptor start ========================
  7. // Trace just calls AddStack.
  8. func Trace(err error) error {
  9. if err == nil {
  10. return nil
  11. }
  12. return AddStack(err)
  13. }
  14. // Annotate adds a message and ensures there is a stack trace.
  15. func Annotate(err error, message string) error {
  16. if err == nil {
  17. return nil
  18. }
  19. hasStack := HasStack(err)
  20. err = &withMessage{
  21. cause: err,
  22. msg: message,
  23. causeHasStack: hasStack,
  24. }
  25. if hasStack {
  26. return err
  27. }
  28. return &withStack{
  29. err,
  30. callers(),
  31. }
  32. }
  33. // Annotatef adds a message and ensures there is a stack trace.
  34. func Annotatef(err error, format string, args ...interface{}) error {
  35. if err == nil {
  36. return nil
  37. }
  38. hasStack := HasStack(err)
  39. err = &withMessage{
  40. cause: err,
  41. msg: fmt.Sprintf(format, args...),
  42. causeHasStack: hasStack,
  43. }
  44. if hasStack {
  45. return err
  46. }
  47. return &withStack{
  48. err,
  49. callers(),
  50. }
  51. }
  52. var emptyStack stack
  53. // NewNoStackError creates error without error stack
  54. // later duplicate trace will no longer generate Stack too.
  55. func NewNoStackError(msg string) error {
  56. return &fundamental{
  57. msg: msg,
  58. stack: &emptyStack,
  59. }
  60. }
  61. // SuspendStack suspends stack generate for error.
  62. func SuspendStack(err error) error {
  63. if err == nil {
  64. return err
  65. }
  66. cleared := clearStack(err)
  67. if cleared {
  68. return err
  69. }
  70. return &withStack{
  71. err,
  72. &emptyStack,
  73. }
  74. }
  75. func clearStack(err error) (cleared bool) {
  76. switch typedErr := err.(type) {
  77. case *withMessage:
  78. return clearStack(typedErr.Cause())
  79. case *fundamental:
  80. typedErr.stack = &emptyStack
  81. return true
  82. case *withStack:
  83. typedErr.stack = &emptyStack
  84. clearStack(typedErr.Cause())
  85. return true
  86. default:
  87. return false
  88. }
  89. }
  90. // ErrorStack will format a stack trace if it is available, otherwise it will be Error()
  91. // If the error is nil, the empty string is returned
  92. // Note that this just calls fmt.Sprintf("%+v", err)
  93. func ErrorStack(err error) string {
  94. if err == nil {
  95. return ""
  96. }
  97. return fmt.Sprintf("%+v", err)
  98. }
  99. // IsNotFound reports whether err was not found error.
  100. func IsNotFound(err error) bool {
  101. return strings.Contains(err.Error(), "not found")
  102. }
  103. // NotFoundf represents an error with not found message.
  104. func NotFoundf(format string, args ...interface{}) error {
  105. return Errorf(format+" not found", args...)
  106. }
  107. // BadRequestf represents an error with bad request message.
  108. func BadRequestf(format string, args ...interface{}) error {
  109. return Errorf(format+" bad request", args...)
  110. }
  111. // NotSupportedf represents an error with not supported message.
  112. func NotSupportedf(format string, args ...interface{}) error {
  113. return Errorf(format+" not supported", args...)
  114. }
  115. // NotValidf represents an error with not valid message.
  116. func NotValidf(format string, args ...interface{}) error {
  117. return Errorf(format+" not valid", args...)
  118. }
  119. // IsAlreadyExists reports whether err was already exists error.
  120. func IsAlreadyExists(err error) bool {
  121. return strings.Contains(err.Error(), "already exists")
  122. }
  123. // AlreadyExistsf represents an error with already exists message.
  124. func AlreadyExistsf(format string, args ...interface{}) error {
  125. return Errorf(format+" already exists", args...)
  126. }
  127. // ==================== juju adaptor end ========================