Debian Codegraph @

errors/fmt/format_example_test.go

raw symbols SCIP
1 // Copyright 2018 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 fmt_test
6
7 import (
8 "path/filepath"
9 "regexp"
10
11 "golang.org/x/exp/errors"
12 "golang.org/x/exp/errors/fmt"
13 )
14
15 func baz() error { return errors.New("baz flopped") }
16 func bar() error { return fmt.Errorf("bar(nameserver 139): %v", baz()) }
17 func foo() error { return fmt.Errorf("foo: %s", bar()) }
18
19 func Example_formatting() {
20 err := foo()
21 fmt.Println("Error:")
22 fmt.Printf("%v\n", err)
23 fmt.Println()
24 fmt.Println("Detailed error:")
25 fmt.Println(stripPath(fmt.Sprintf("%+v\n", err)))
26 // Output:
27 // Error:
28 // foo: bar(nameserver 139): baz flopped
29 //
30 // Detailed error:
31 // foo:
32 // golang.org/x/exp/errors/fmt_test.foo
33 // golang.org/x/exp/errors/fmt/format_example_test.go:17
34 // - bar(nameserver 139):
35 // golang.org/x/exp/errors/fmt_test.bar
36 // golang.org/x/exp/errors/fmt/format_example_test.go:16
37 // - baz flopped:
38 // golang.org/x/exp/errors/fmt_test.baz
39 // golang.org/x/exp/errors/fmt/format_example_test.go:15
40 }
41
42 func stripPath(s string) string {
43 rePath := regexp.MustCompile(`( [^ ]*)/fmt`)
44 s = rePath.ReplaceAllString(s, " golang.org/x/exp/errors/fmt")
45 s = filepath.ToSlash(s)
46 return s
47 }