error.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zapcore
  21. import (
  22. "fmt"
  23. "sync"
  24. )
  25. // Encodes the given error into fields of an object. A field with the given
  26. // name is added for the error message.
  27. //
  28. // If the error implements fmt.Formatter, a field with the name ${key}Verbose
  29. // is also added with the full verbose error message.
  30. //
  31. // Finally, if the error implements errorGroup (from go.uber.org/multierr) or
  32. // causer (from github.com/pkg/errors), a ${key}Causes field is added with an
  33. // array of objects containing the errors this error was comprised of.
  34. //
  35. // {
  36. // "error": err.Error(),
  37. // "errorVerbose": fmt.Sprintf("%+v", err),
  38. // "errorCauses": [
  39. // ...
  40. // ],
  41. // }
  42. func encodeError(key string, err error, enc ObjectEncoder) error {
  43. basic := err.Error()
  44. enc.AddString(key, basic)
  45. switch e := err.(type) {
  46. case errorGroup:
  47. return enc.AddArray(key+"Causes", errArray(e.Errors()))
  48. case fmt.Formatter:
  49. verbose := fmt.Sprintf("%+v", e)
  50. if verbose != basic {
  51. // This is a rich error type, like those produced by
  52. // github.com/pkg/errors.
  53. enc.AddString(key+"Verbose", verbose)
  54. }
  55. }
  56. return nil
  57. }
  58. type errorGroup interface {
  59. // Provides read-only access to the underlying list of errors, preferably
  60. // without causing any allocs.
  61. Errors() []error
  62. }
  63. // Note that errArry and errArrayElem are very similar to the version
  64. // implemented in the top-level error.go file. We can't re-use this because
  65. // that would require exporting errArray as part of the zapcore API.
  66. // Encodes a list of errors using the standard error encoding logic.
  67. type errArray []error
  68. func (errs errArray) MarshalLogArray(arr ArrayEncoder) error {
  69. for i := range errs {
  70. if errs[i] == nil {
  71. continue
  72. }
  73. el := newErrArrayElem(errs[i])
  74. arr.AppendObject(el)
  75. el.Free()
  76. }
  77. return nil
  78. }
  79. var _errArrayElemPool = sync.Pool{New: func() interface{} {
  80. return &errArrayElem{}
  81. }}
  82. // Encodes any error into a {"error": ...} re-using the same errors logic.
  83. //
  84. // May be passed in place of an array to build a single-element array.
  85. type errArrayElem struct{ err error }
  86. func newErrArrayElem(err error) *errArrayElem {
  87. e := _errArrayElemPool.Get().(*errArrayElem)
  88. e.err = err
  89. return e
  90. }
  91. func (e *errArrayElem) MarshalLogArray(arr ArrayEncoder) error {
  92. return arr.AppendObject(e)
  93. }
  94. func (e *errArrayElem) MarshalLogObject(enc ObjectEncoder) error {
  95. return encodeError("error", e.err, enc)
  96. }
  97. func (e *errArrayElem) Free() {
  98. e.err = nil
  99. _errArrayElemPool.Put(e)
  100. }