error.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 zap
  21. import (
  22. "sync"
  23. "go.uber.org/zap/zapcore"
  24. )
  25. var _errArrayElemPool = sync.Pool{New: func() interface{} {
  26. return &errArrayElem{}
  27. }}
  28. // Error is shorthand for the common idiom NamedError("error", err).
  29. func Error(err error) Field {
  30. return NamedError("error", err)
  31. }
  32. // NamedError constructs a field that lazily stores err.Error() under the
  33. // provided key. Errors which also implement fmt.Formatter (like those produced
  34. // by github.com/pkg/errors) will also have their verbose representation stored
  35. // under key+"Verbose". If passed a nil error, the field is a no-op.
  36. //
  37. // For the common case in which the key is simply "error", the Error function
  38. // is shorter and less repetitive.
  39. func NamedError(key string, err error) Field {
  40. if err == nil {
  41. return Skip()
  42. }
  43. return Field{Key: key, Type: zapcore.ErrorType, Interface: err}
  44. }
  45. type errArray []error
  46. func (errs errArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  47. for i := range errs {
  48. if errs[i] == nil {
  49. continue
  50. }
  51. // To represent each error as an object with an "error" attribute and
  52. // potentially an "errorVerbose" attribute, we need to wrap it in a
  53. // type that implements LogObjectMarshaler. To prevent this from
  54. // allocating, pool the wrapper type.
  55. elem := _errArrayElemPool.Get().(*errArrayElem)
  56. elem.error = errs[i]
  57. arr.AppendObject(elem)
  58. elem.error = nil
  59. _errArrayElemPool.Put(elem)
  60. }
  61. return nil
  62. }
  63. type errArrayElem struct {
  64. error
  65. }
  66. func (e *errArrayElem) MarshalLogObject(enc zapcore.ObjectEncoder) error {
  67. // Re-use the error field's logic, which supports non-standard error types.
  68. Error(e.error).AddTo(enc)
  69. return nil
  70. }