Last active
March 5, 2019 06:44
-
-
Save ques0942/d2e5e55780b08653adde602ffb247925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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