You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.1 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "fmt"
  4. "runtime"
  5. )
  6. //----------------------------------------
  7. // Error & cmnError
  8. type Error interface {
  9. Error() string
  10. Trace(msg string) Error
  11. TraceCause(cause error, msg string) Error
  12. Cause() error
  13. }
  14. func NewError(msg string) Error {
  15. return newError(msg)
  16. }
  17. type traceItem struct {
  18. msg string
  19. filename string
  20. lineno int
  21. }
  22. func (ti traceItem) String() string {
  23. return fmt.Sprintf("%v:%v %v", ti.filename, ti.lineno, ti.msg)
  24. }
  25. type cmnError struct {
  26. msg string
  27. cause error
  28. traces []traceItem
  29. }
  30. func newError(msg string) *cmnError {
  31. return &cmnError{
  32. msg: msg,
  33. cause: nil,
  34. traces: nil,
  35. }
  36. }
  37. func (err *cmnError) Error() string {
  38. return fmt.Sprintf("Error{%s,%v,%v}", err.msg, err.cause, len(err.traces))
  39. }
  40. // Add tracing information with msg.
  41. func (err *cmnError) Trace(msg string) Error {
  42. return err.doTrace(msg, 2)
  43. }
  44. // Add tracing information with cause and msg.
  45. // If a cause was already set before, it is overwritten.
  46. func (err *cmnError) TraceCause(cause error, msg string) Error {
  47. err.cause = cause
  48. return err.doTrace(msg, 2)
  49. }
  50. func (err *cmnError) doTrace(msg string, n int) Error {
  51. _, fn, line, ok := runtime.Caller(n)
  52. if !ok {
  53. if fn == "" {
  54. fn = "<unknown>"
  55. }
  56. if line <= 0 {
  57. line = -1
  58. }
  59. }
  60. // Include file & line number & msg.
  61. // Do not include the whole stack trace.
  62. err.traces = append(err.traces, traceItem{
  63. filename: fn,
  64. lineno: line,
  65. msg: msg,
  66. })
  67. return err
  68. }
  69. // Return last known cause.
  70. // NOTE: The meaning of "cause" is left for the caller to define.
  71. // There exists to canonical definition of "cause".
  72. // Instead of blaming, try to handle-or-organize it.
  73. func (err *cmnError) Cause() error {
  74. return err.cause
  75. }
  76. //----------------------------------------
  77. // StackError
  78. // NOTE: Used by Tendermint p2p upon recovery.
  79. // Err could be "Reason", since it isn't an error type.
  80. type StackError struct {
  81. Err interface{}
  82. Stack []byte
  83. }
  84. func (se StackError) String() string {
  85. return fmt.Sprintf("Error: %v\nStack: %s", se.Err, se.Stack)
  86. }
  87. func (se StackError) Error() string {
  88. return se.String()
  89. }
  90. //----------------------------------------
  91. // Panic wrappers
  92. // XXX DEPRECATED
  93. // A panic resulting from a sanity check means there is a programmer error
  94. // and some guarantee is not satisfied.
  95. // XXX DEPRECATED
  96. func PanicSanity(v interface{}) {
  97. panic(Fmt("Panicked on a Sanity Check: %v", v))
  98. }
  99. // A panic here means something has gone horribly wrong, in the form of data corruption or
  100. // failure of the operating system. In a correct/healthy system, these should never fire.
  101. // If they do, it's indicative of a much more serious problem.
  102. // XXX DEPRECATED
  103. func PanicCrisis(v interface{}) {
  104. panic(Fmt("Panicked on a Crisis: %v", v))
  105. }
  106. // Indicates a failure of consensus. Someone was malicious or something has
  107. // gone horribly wrong. These should really boot us into an "emergency-recover" mode
  108. // XXX DEPRECATED
  109. func PanicConsensus(v interface{}) {
  110. panic(Fmt("Panicked on a Consensus Failure: %v", v))
  111. }
  112. // For those times when we're not sure if we should panic
  113. // XXX DEPRECATED
  114. func PanicQ(v interface{}) {
  115. panic(Fmt("Panicked questionably: %v", v))
  116. }