errors.go 815 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xerrors
  5. import "fmt"
  6. // errorString is a trivial implementation of error.
  7. type errorString struct {
  8. s string
  9. frame Frame
  10. }
  11. // New returns an error that formats as the given text.
  12. //
  13. // The returned error contains a Frame set to the caller's location and
  14. // implements Formatter to show this information when printed with details.
  15. func New(text string) error {
  16. return &errorString{text, Caller(1)}
  17. }
  18. func (e *errorString) Error() string {
  19. return e.s
  20. }
  21. func (e *errorString) Format(s fmt.State, v rune) { FormatError(e, s, v) }
  22. func (e *errorString) FormatError(p Printer) (next error) {
  23. p.Print(e.s)
  24. e.frame.Format(p)
  25. return nil
  26. }