errors.go 2.9 KB

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