Command line client for Write.as https://write.as/apps/cli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.2 KiB

  1. package multierror
  2. import (
  3. "fmt"
  4. )
  5. // Error is an error type to track multiple errors. This is used to
  6. // accumulate errors in cases and return them as a single "error".
  7. type Error struct {
  8. Errors []error
  9. ErrorFormat ErrorFormatFunc
  10. }
  11. func (e *Error) Error() string {
  12. fn := e.ErrorFormat
  13. if fn == nil {
  14. fn = ListFormatFunc
  15. }
  16. return fn(e.Errors)
  17. }
  18. // ErrorOrNil returns an error interface if this Error represents
  19. // a list of errors, or returns nil if the list of errors is empty. This
  20. // function is useful at the end of accumulation to make sure that the value
  21. // returned represents the existence of errors.
  22. func (e *Error) ErrorOrNil() error {
  23. if e == nil {
  24. return nil
  25. }
  26. if len(e.Errors) == 0 {
  27. return nil
  28. }
  29. return e
  30. }
  31. func (e *Error) GoString() string {
  32. return fmt.Sprintf("*%#v", *e)
  33. }
  34. // WrappedErrors returns the list of errors that this Error is wrapping.
  35. // It is an implementation of the errwrap.Wrapper interface so that
  36. // multierror.Error can be used with that library.
  37. //
  38. // This method is not safe to be called concurrently and is no different
  39. // than accessing the Errors field directly. It is implemented only to
  40. // satisfy the errwrap.Wrapper interface.
  41. func (e *Error) WrappedErrors() []error {
  42. return e.Errors
  43. }