package fserr import ( "fmt" "io" ) type fundamental struct { msg string *stack } func (f *fundamental) Error() string { return f.msg } func (f *fundamental) Format(s fmt.State, verb rune) { switch verb { case 'v': if s.Flag('+') { io.WriteString(s, f.msg) f.stack.Format(s, verb) return } fallthrough case 's': io.WriteString(s, f.msg) case 'q': fmt.Fprintf(s, "%q", f.msg) } } type withStack struct { error *stack } func (w *withStack) Cause() error { return w.error } func (w *withStack) Unwrap() error { return w.error } func (w *withStack) Format(s fmt.State, verb rune) { switch verb { case 'v': if s.Flag('+') { if w.Cause() != nil { fmt.Fprintf(s, "%+v", w.Cause()) } w.stack.Format(s, verb) return } fallthrough case 's': io.WriteString(s, w.Error()) case 'q': fmt.Fprintf(s, "%q", w.Error()) } } type withMessage struct { cause error msg string } func (w *withMessage) Error() string { return w.msg } func (w *withMessage) Cause() error { return w.cause } func (w *withMessage) Unwrap() error { return w.cause } func (w *withMessage) Format(s fmt.State, verb rune) { switch verb { case 'v': if s.Flag('+') { io.WriteString(s, w.msg+"\n") if w.Cause() != nil { fmt.Fprintf(s, "%+v", w.Cause()) } return } fallthrough case 's': io.WriteString(s, w.Error()) case 'q': fmt.Fprintf(s, "%q", w.Error()) } } type withCode struct { cause error Msg string `json:"msg"` HttpCode int `json:"httpCode"` BusinessCode int `json:"businessCode"` } func (w *withCode) Error() string { return w.Msg } func (w *withCode) Cause() error { return w.cause } func (w *withCode) Unwrap() error { return w.cause } func (w *withCode) Format(s fmt.State, verb rune) { switch verb { case 'v': if s.Flag('+') { io.WriteString(s, w.Msg+"\n") if w.Cause() != nil { fmt.Fprintf(s, "%+v", w.Cause()) } return } fallthrough case 's': io.WriteString(s, w.Error()) case 'q': fmt.Fprintf(s, "%q", w.Error()) } }