diff options
| author | Marcel van Lohuizen <mpvl@golang.org> | 2019-02-23 00:29:15 +0100 |
|---|---|---|
| committer | Marcel van Lohuizen <mpvl@golang.org> | 2019-02-27 19:29:14 +0000 |
| commit | 6be6f114e0d483a233101a67c9644cd72bd3ae7a (patch) | |
| tree | 7860e6c34e77f2e2a8390dfc91f1db77028e70cf /src/fmt/format_example_test.go | |
| parent | 62f5e8156ef56fa61e6af56f4ccc633bde1a9120 (diff) | |
| download | go-6be6f114e0d483a233101a67c9644cd72bd3ae7a.tar.xz | |
fmt: add frame info to Errorf and support %w
Partly implements proposal Issue #29934.
Change-Id: Ibcf12f383158dcfbc313ab29c417a710571d1acb
Reviewed-on: https://go-review.googlesource.com/c/163559
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/fmt/format_example_test.go')
| -rw-r--r-- | src/fmt/format_example_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/fmt/format_example_test.go b/src/fmt/format_example_test.go new file mode 100644 index 0000000000..386f10ef23 --- /dev/null +++ b/src/fmt/format_example_test.go @@ -0,0 +1,46 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fmt_test + +import ( + "errors" + "fmt" + "path/filepath" + "regexp" +) + +func baz() error { return errors.New("baz flopped") } +func bar() error { return fmt.Errorf("bar(nameserver 139): %v", baz()) } +func foo() error { return fmt.Errorf("foo: %s", bar()) } + +func Example_formatting() { + err := foo() + fmt.Println("Error:") + fmt.Printf("%v\n", err) + fmt.Println() + fmt.Println("Detailed error:") + fmt.Println(stripPath(fmt.Sprintf("%+v\n", err))) + // Output: + // Error: + // foo: bar(nameserver 139): baz flopped + // + // Detailed error: + // foo: + // fmt_test.foo + // fmt/format_example_test.go:16 + // - bar(nameserver 139): + // fmt_test.bar + // fmt/format_example_test.go:15 + // - baz flopped: + // fmt_test.baz + // fmt/format_example_test.go:14 +} + +func stripPath(s string) string { + rePath := regexp.MustCompile(`( [^ ]*)fmt`) + s = rePath.ReplaceAllString(s, " fmt") + s = filepath.ToSlash(s) + return s +} |
