errors.go raw

   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  
   5  package xerrors
   6  
   7  import "fmt"
   8  
   9  // errorString is a trivial implementation of error.
  10  type errorString struct {
  11  	s     string
  12  	frame Frame
  13  }
  14  
  15  // New returns an error that formats as the given text.
  16  //
  17  // The returned error contains a Frame set to the caller's location and
  18  // implements Formatter to show this information when printed with details.
  19  func New(text string) error {
  20  	return &errorString{text, Caller(1)}
  21  }
  22  
  23  func (e *errorString) Error() string {
  24  	return e.s
  25  }
  26  
  27  func (e *errorString) Format(s fmt.State, v rune) { FormatError(e, s, v) }
  28  
  29  func (e *errorString) FormatError(p Printer) (next error) {
  30  	p.Print(e.s)
  31  	e.frame.Format(p)
  32  	return nil
  33  }
  34