github.com/nikandfor/errors@v0.8.0/fmt.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 ) 6 7 // Format formats error message and adds location if present. 8 // 9 // fmt.Formatter interface implementation. 10 func (e wrapper) Format(s fmt.State, c rune) { 11 e.formatMain(s, c) 12 e.formatSub(s, c, true) 13 } 14 15 func (e wrapper) formatMain(s fmt.State, c rune) { 16 if e.msg == "" { 17 e.msg = nomessage 18 } 19 20 fmt.Fprintf(s, "%s", e.msg) 21 } 22 23 func (e wrapper) formatSub(s fmt.State, c rune, delim bool) { 24 if e.err == nil { 25 return 26 } 27 28 if delim { 29 if s.Flag(' ') { 30 _, _ = s.Write([]byte{'\n'}) 31 } else { 32 _, _ = s.Write([]byte{':', ' '}) 33 } 34 } 35 36 subPrintArg(s, e.err, c) 37 } 38 39 func (e withPC) Format(s fmt.State, c rune) { 40 if e.msg != "" || s.Flag('+') || e.err == nil { 41 e.wrapper.formatMain(s, c) 42 } 43 44 if e.pc != 0 && s.Flag('+') { 45 if s.Flag(' ') { 46 fmt.Fprintf(s, " at %+v", e.pc) 47 } else { 48 fmt.Fprintf(s, " (%v)", e.pc) 49 } 50 } 51 52 e.wrapper.formatSub(s, c, e.msg != "" || s.Flag('+')) 53 }