Skip to content

Instantly share code, notes, and snippets.

@ques0942
Last active March 5, 2019 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ques0942/d2e5e55780b08653adde602ffb247925 to your computer and use it in GitHub Desktop.
Save ques0942/d2e5e55780b08653adde602ffb247925 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"golang.org/x/xerrors"
)
type printer struct {
values []string
function interface{}
file interface{}
line interface{}
}
var _ xerrors.Printer = &printer{}
func (p *printer) Print(args ...interface{}) {
}
func (p *printer) Printf(format string, args ...interface{}) {
if format == "%s\n " {
p.function = args[0]
}
if format == "%s:%d\n" {
p.file = args[0]
p.line = args[1]
}
}
func (p *printer) Detail() bool {
return true
}
func err() error {
return xerrors.Errorf("err")
}
func main() {
err := xerrors.Errorf("test: %w", err())
var printers []printer
e := err
for e != nil {
if fmtr, ok := e.(xerrors.Formatter); ok {
p := printer{}
e = fmtr.FormatError(&p)
printers = append(printers, p)
} else {
break
}
}
for i, _ := range printers {
fmt.Printf("function: %s, file: %s, line: %d\n", printers[i].function, printers[i].file, printers[i].line)
}
fmt.Printf("%+v\n", err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment