errors.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "runtime"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/stacktrace"
  28. )
  29. // TraceNew returns a new error with the given message, wrapped with the caller
  30. // stack frame information.
  31. func TraceNew(message string) error {
  32. err := fmt.Errorf("%s", message)
  33. pc, _, line, ok := runtime.Caller(1)
  34. if !ok {
  35. return fmt.Errorf("[unknown]: %w", err)
  36. }
  37. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  38. }
  39. // BackTraceNew returns a new error with the given message, wrapped with the
  40. // caller stack frame information going back up the stack until the caller of
  41. // the specified function name is encountered.
  42. func BackTraceNew(backTraceFuncName, message string) error {
  43. err := fmt.Errorf("%s", message)
  44. return fmt.Errorf("%s%w", backTrace(backTraceFuncName), err)
  45. }
  46. // Tracef returns a new error with the given formatted message, wrapped with
  47. // the caller stack frame information.
  48. func Tracef(format string, args ...interface{}) error {
  49. err := fmt.Errorf(format, args...)
  50. pc, _, line, ok := runtime.Caller(1)
  51. if !ok {
  52. return fmt.Errorf("[unknown]: %w", err)
  53. }
  54. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  55. }
  56. // Trace wraps the given error with the caller stack frame information.
  57. func Trace(err error) error {
  58. if err == nil {
  59. return nil
  60. }
  61. pc, _, line, ok := runtime.Caller(1)
  62. if !ok {
  63. return fmt.Errorf("[unknown]: %w", err)
  64. }
  65. return fmt.Errorf("%s#%d: %w", stacktrace.GetFunctionName(pc), line, err)
  66. }
  67. // TraceMsg wraps the given error with the caller stack frame information
  68. // and the given message.
  69. func TraceMsg(err error, message string) error {
  70. if err == nil {
  71. return nil
  72. }
  73. pc, _, line, ok := runtime.Caller(1)
  74. if !ok {
  75. return fmt.Errorf("[unknown]: %s: %w", message, err)
  76. }
  77. return fmt.Errorf("%s#%d: %s: %w", stacktrace.GetFunctionName(pc), line, message, err)
  78. }
  79. func backTrace(backTraceFuncName string) string {
  80. stop := false
  81. trace := ""
  82. // Skip starts at 2, assuming backTrace is called as a helper function.
  83. for n := 2; ; n++ {
  84. pc, _, line, ok := runtime.Caller(n)
  85. if !ok {
  86. break
  87. }
  88. funcName := stacktrace.GetFunctionName(pc)
  89. trace = fmt.Sprintf("%s#%d: ", funcName, line) + trace
  90. if stop {
  91. break
  92. }
  93. if funcName == backTraceFuncName {
  94. // Stop after the _next_ function
  95. stop = true
  96. }
  97. }
  98. return trace
  99. }