errors.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package fserr
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. type fundamental struct {
  7. msg string
  8. *stack
  9. }
  10. func (f *fundamental) Error() string { return f.msg }
  11. func (f *fundamental) Format(s fmt.State, verb rune) {
  12. switch verb {
  13. case 'v':
  14. if s.Flag('+') {
  15. _, _ = io.WriteString(s, f.msg)
  16. f.stack.Format(s, verb)
  17. return
  18. }
  19. fallthrough
  20. case 's':
  21. _, _ = io.WriteString(s, f.msg)
  22. case 'q':
  23. _, _ = fmt.Fprintf(s, "%q", f.msg)
  24. }
  25. }
  26. type withStack struct {
  27. error
  28. *stack
  29. }
  30. func (w *withStack) Cause() error { return w.error }
  31. func (w *withStack) Unwrap() error { return w.error }
  32. func (w *withStack) Format(s fmt.State, verb rune) {
  33. switch verb {
  34. case 'v':
  35. if s.Flag('+') {
  36. if w.Cause() != nil {
  37. _, _ = fmt.Fprintf(s, "%+v", w.Cause())
  38. }
  39. w.stack.Format(s, verb)
  40. return
  41. }
  42. fallthrough
  43. case 's':
  44. _, _ = io.WriteString(s, w.Error())
  45. case 'q':
  46. _, _ = fmt.Fprintf(s, "%q", w.Error())
  47. }
  48. }
  49. type withMessage struct {
  50. cause error
  51. msg string
  52. }
  53. func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
  54. func (w *withMessage) Cause() error { return w.cause }
  55. func (w *withMessage) Unwrap() error { return w.cause }
  56. func (w *withMessage) Format(s fmt.State, verb rune) {
  57. switch verb {
  58. case 'v':
  59. if s.Flag('+') {
  60. _, _ = io.WriteString(s, w.msg+": ")
  61. if w.Cause() != nil {
  62. _, _ = fmt.Fprintf(s, "%+v", w.Cause())
  63. }
  64. return
  65. }
  66. fallthrough
  67. case 's':
  68. _, _ = io.WriteString(s, w.Error())
  69. case 'q':
  70. _, _ = fmt.Fprintf(s, "%q", w.Error())
  71. }
  72. }
  73. type withCode struct {
  74. cause error
  75. Msg string `json:"msg"`
  76. HttpCode int `json:"httpCode"`
  77. BusinessCode int `json:"businessCode"`
  78. }
  79. func (w *withCode) Error() string { return w.Msg }
  80. func (w *withCode) Cause() error { return w.cause }
  81. func (w *withCode) Unwrap() error { return w.cause }
  82. func (w *withCode) Format(s fmt.State, verb rune) {
  83. switch verb {
  84. case 'v':
  85. if s.Flag('+') {
  86. _, _ = io.WriteString(s, w.Msg+"\n")
  87. if w.Cause() != nil {
  88. _, _ = fmt.Fprintf(s, "%+v", w.Cause())
  89. }
  90. return
  91. }
  92. fallthrough
  93. case 's':
  94. _, _ = io.WriteString(s, w.Error())
  95. case 'q':
  96. _, _ = fmt.Fprintf(s, "%q", w.Error())
  97. }
  98. }