@@ -66,14 +66,14 @@ func (w *withMessage) Wrapf(err error, format string, args ...any) error {
66
66
if err == nil {
67
67
return nil
68
68
}
69
- wm := & withMessage {
70
- message : w . Message ( ),
69
+ wm := withMessage {
70
+ message : fmt . Sprintf ( format , args ... ),
71
71
cause : err ,
72
72
}
73
73
ws := & withStack {
74
74
error : & withMessage {
75
- message : fmt . Sprintf ( format , args ... ) ,
76
- cause : wm ,
75
+ message : w . message ,
76
+ cause : & wm ,
77
77
},
78
78
}
79
79
if ! stackExists (ws ) {
@@ -104,26 +104,37 @@ type ErrorCode interface {
104
104
}
105
105
106
106
type withCode struct {
107
- * withMessage
108
- code int
107
+ code int
108
+ message string
109
+ cause error
109
110
}
110
111
111
112
func (w * withCode ) Code () int {
112
113
return w .code
113
114
}
114
115
116
+ func (w * withCode ) Message () string {
117
+ return w .message
118
+ }
119
+
120
+ func (w * withCode ) Cause () error {
121
+ return w .cause
122
+ }
123
+
124
+ func (w * withCode ) Unwrap () error {
125
+ return w .cause
126
+ }
127
+
115
128
// WrapStack If err is nil, WithStack returns nil.
116
129
func (w * withCode ) WrapStack (err error ) error {
117
130
if err == nil {
118
131
return nil
119
132
}
120
133
ws := & withStack {
121
134
error : & withCode {
122
- withMessage : & withMessage {
123
- message : w .Message (),
124
- cause : err ,
125
- },
126
- code : w .Code (),
135
+ code : w .code ,
136
+ message : w .message ,
137
+ cause : err ,
127
138
},
128
139
}
129
140
if ! stackExists (ws ) {
@@ -137,19 +148,17 @@ func (w *withCode) Wrapf(err error, format string, args ...any) error {
137
148
if err == nil {
138
149
return nil
139
150
}
140
- wm := & withMessage {
141
- message : w . Message ( ),
151
+ wm := withMessage {
152
+ message : fmt . Sprintf ( format , args ... ),
142
153
cause : err ,
143
154
}
144
- wc := & withCode {
145
- withMessage : wm ,
146
- code : w .Code (),
155
+ wc := withCode {
156
+ code : w .code ,
157
+ message : w .message ,
158
+ cause : & wm ,
147
159
}
148
160
ws := & withStack {
149
- error : & withMessage {
150
- message : fmt .Sprintf (format , args ... ),
151
- cause : wc ,
152
- },
161
+ error : & wc ,
153
162
}
154
163
if ! stackExists (ws ) {
155
164
ws .stack = callers ()
@@ -158,11 +167,11 @@ func (w *withCode) Wrapf(err error, format string, args ...any) error {
158
167
}
159
168
160
169
func (w * withCode ) Error () string {
161
- s := fmt .Sprintf ("[%d: %s]" , w .Code () , w .Message () )
162
- if w .Cause () == nil {
170
+ s := fmt .Sprintf ("[%d: %s]" , w .code , w .message )
171
+ if w .cause == nil {
163
172
return s
164
173
}
165
- return fmt .Sprintf ("%s -> {%s}" , s , w .Cause () )
174
+ return fmt .Sprintf ("%s -> {%s}" , s , w .cause )
166
175
}
167
176
168
177
func (w * withCode ) Is (err error ) bool {
@@ -176,10 +185,10 @@ func (w *withCode) Format(s fmt.State, verb rune) {
176
185
switch verb {
177
186
case 'v' :
178
187
if s .Flag ('+' ) {
179
- if w .Cause () != nil {
180
- fmt .Fprintf (s , "%+v\n " , w .Cause () )
188
+ if w .cause != nil {
189
+ fmt .Fprintf (s , "%+v\n " , w .cause )
181
190
}
182
- fmt .Fprintf (s , "%d: %s" , w .Code () , w .Message () )
191
+ fmt .Fprintf (s , "%d: %s" , w .code , w .message )
183
192
return
184
193
}
185
194
fallthrough
@@ -273,10 +282,8 @@ func NewWithMessage(format string, args ...any) ErrorMessage {
273
282
// NewWithCode no stack
274
283
func NewWithCode (code int , format string , args ... any ) ErrorCode {
275
284
return & withCode {
276
- code : code ,
277
- withMessage : & withMessage {
278
- message : fmt .Sprintf (format , args ... ),
279
- },
285
+ code : code ,
286
+ message : fmt .Sprintf (format , args ... ),
280
287
}
281
288
}
282
289
0 commit comments