new.go 801 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package xerrors
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/ydb-platform/ydb/library/go/x/xruntime"
  6. )
  7. type newError struct {
  8. msg string
  9. stacktrace *xruntime.StackTrace
  10. }
  11. var _ ErrorStackTrace = &newError{}
  12. func New(text string) error {
  13. return &newError{
  14. msg: text,
  15. stacktrace: newStackTrace(1, nil),
  16. }
  17. }
  18. func (e *newError) Error() string {
  19. return e.msg
  20. }
  21. func (e *newError) Format(s fmt.State, v rune) {
  22. switch v {
  23. case 'v':
  24. if s.Flag('+') && e.stacktrace != nil {
  25. _, _ = io.WriteString(s, e.msg)
  26. _, _ = io.WriteString(s, "\n")
  27. writeStackTrace(s, e.stacktrace)
  28. return
  29. }
  30. fallthrough
  31. case 's':
  32. _, _ = io.WriteString(s, e.msg)
  33. case 'q':
  34. _, _ = fmt.Fprintf(s, "%q", e.msg)
  35. }
  36. }
  37. func (e *newError) StackTrace() *xruntime.StackTrace {
  38. return e.stacktrace
  39. }