errors.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2019, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /*
  20. Package errors provides error wrapping helpers that add inline, single frame
  21. stack trace information to error messages.
  22. */
  23. package errors
  24. import (
  25. "fmt"
  26. "io"
  27. "runtime"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/stacktrace"
  29. )
  30. // TraceNew returns a new error with the given message, wrapped with the caller
  31. // stack frame information.
  32. func TraceNew(message string) error {
  33. err := fmt.Errorf("%s", message)
  34. pc, _, line, ok := runtime.Caller(1)
  35. if !ok {
  36. return fmt.Errorf("[unknown]: %w", err)
  37. }
  38. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  39. }
  40. // BackTraceNew returns a new error with the given message, wrapped with the
  41. // caller stack frame information going back up the stack until the caller of
  42. // the specified function name is encountered.
  43. func BackTraceNew(backTraceFuncName, message string) error {
  44. err := fmt.Errorf("%s", message)
  45. return fmt.Errorf("%s%w", backTrace(backTraceFuncName), err)
  46. }
  47. // Tracef returns a new error with the given formatted message, wrapped with
  48. // the caller stack frame information.
  49. func Tracef(format string, args ...interface{}) error {
  50. err := fmt.Errorf(format, args...)
  51. pc, _, line, ok := runtime.Caller(1)
  52. if !ok {
  53. return fmt.Errorf("[unknown]: %w", err)
  54. }
  55. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  56. }
  57. // Trace wraps the given error with the caller stack frame information.
  58. func Trace(err error) error {
  59. if err == nil {
  60. return nil
  61. }
  62. pc, _, line, ok := runtime.Caller(1)
  63. if !ok {
  64. return fmt.Errorf("[unknown]: %w", err)
  65. }
  66. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  67. }
  68. // TraceReader wraps the given error with the caller stack frame information,
  69. // except in the case of io.EOF, which is returned unwrapped. This is used to
  70. // preserve io.Reader.Read io.EOF error returns.
  71. func TraceReader(err error) error {
  72. if err == nil {
  73. return nil
  74. }
  75. if err == io.EOF {
  76. return io.EOF
  77. }
  78. pc, _, line, ok := runtime.Caller(1)
  79. if !ok {
  80. return fmt.Errorf("[unknown]: %w", err)
  81. }
  82. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  83. }
  84. // TraceMsg wraps the given error with the caller stack frame information
  85. // and the given message.
  86. func TraceMsg(err error, message string) error {
  87. if err == nil {
  88. return nil
  89. }
  90. pc, _, line, ok := runtime.Caller(1)
  91. if !ok {
  92. return fmt.Errorf("[unknown]: %s: %w", message, err)
  93. }
  94. return fmt.Errorf("%s#%d: %s: %w", stacktrace.GetFunctionName(pc), line, message, err)
  95. }
  96. func backTrace(backTraceFuncName string) string {
  97. stop := false
  98. trace := ""
  99. // Skip starts at 2, assuming backTrace is called as a helper function.
  100. for n := 2; ; n++ {
  101. pc, _, line, ok := runtime.Caller(n)
  102. if !ok {
  103. break
  104. }
  105. funcName := stacktrace.GetFunctionName(pc)
  106. trace = fmt.Sprintf("%s#%d: ", funcName, line) + trace
  107. if stop {
  108. break
  109. }
  110. if funcName == backTraceFuncName {
  111. // Stop after the _next_ function
  112. stop = true
  113. }
  114. }
  115. return trace
  116. }