| 1 |
// Copyright 2019 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_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"golang.org/x/xerrors" |
| 11 |
) |
| 12 |
|
| 13 |
type MyError2 struct { |
| 14 |
Message string |
| 15 |
frame xerrors.Frame |
| 16 |
} |
| 17 |
|
| 18 |
func (m *MyError2) Error() string { |
| 19 |
return m.Message |
| 20 |
} |
| 21 |
|
| 22 |
func (m *MyError2) Format(f fmt.State, c rune) { // implements fmt.Formatter |
| 23 |
xerrors.FormatError(m, f, c) |
| 24 |
} |
| 25 |
|
| 26 |
func (m *MyError2) FormatError(p xerrors.Printer) error { // implements xerrors.Formatter |
| 27 |
p.Print(m.Message) |
| 28 |
if p.Detail() { |
| 29 |
m.frame.Format(p) |
| 30 |
} |
| 31 |
return nil |
| 32 |
} |
| 33 |
|
| 34 |
func ExampleFormatError() { |
| 35 |
err := &MyError2{Message: "oops", frame: xerrors.Caller(1)} |
| 36 |
fmt.Printf("%v\n", err) |
| 37 |
fmt.Println() |
| 38 |
fmt.Printf("%+v\n", err) |
| 39 |
} |