@@ -60,6 +60,21 @@ Or you might just want to change it later to error return:
60
60
61
61
Please see the documentation and examples of [Result], [Result1], and [Result2]
62
62
types and their methods.
63
+
64
+ # try.T — Checking and Annotation
65
+
66
+ The try package offers functions [T], [T1], [T2], and [T3] to allow fast
67
+ incremental code refactoring. For example, if you want to add an error check
68
+ specific annotation to the error check already done with [To1]:
69
+
70
+ try.To1(io.Copy(w, r))
71
+
72
+ you can easily change it to [T1] and give extra message added to the error:
73
+
74
+ try.T1(io.Copy(w, r))("error during stream copy")
75
+
76
+ The T functions are offered mainly to allow faste feedback loop to play with the
77
+ error messages and see what works the best.
63
78
*/
64
79
package try
65
80
@@ -245,54 +260,48 @@ func IsNotEnabled(err error) bool {
245
260
// T is similar as [To] but it let's you to annotate a possible error at place.
246
261
//
247
262
// try.T(f.Close)("annotations")
248
- func T (err error ) func (fs string , a ... any ) {
249
- return func (fs string , a ... any ) {
250
- // NOTE if block cannot be refactored 'because it wouldn't inline
251
- // then this whole function!
252
- if err != nil {
253
- er := fmt .Errorf (fs + handler .WrapError , append (a [1 :], err )... )
254
- panic (er )
263
+ func T (err error ) func (fs string ) {
264
+ return func (fs string ) {
265
+ if err == nil {
266
+ return
255
267
}
268
+ panic (annotateErr (err , fs ))
256
269
}
257
270
}
258
271
259
272
// T1 is similar as [To1] but it let's you to annotate a possible error at place.
260
273
//
261
274
// f := try.T1(os.Open("filename")("cannot open cfg file")
262
- func T1 [T any ](v T , err error ) func (fs string , a ... any ) T {
263
- return func (fs string , a ... any ) T {
264
- // NOTE if block cannot be refactored 'because it wouldn't inline
265
- // then this whole function!
266
- if err != nil {
267
- er := fmt .Errorf (fs + handler .WrapError , append (a [1 :], err )... )
268
- panic (er )
275
+ func T1 [T any ](v T , err error ) func (fs string ) T {
276
+ return func (fs string ) T {
277
+ if err == nil {
278
+ return v
269
279
}
270
- return v
280
+ panic ( annotateErr ( err , fs ))
271
281
}
272
282
}
273
283
274
284
// T2 is similar as [To2] but it let's you to annotate a possible error at place.
275
- func T2 [T , U any ](v T , u U , err error ) func (fs string , a ... any ) (T , U ) {
276
- return func (fs string , a ... any ) (T , U ) {
277
- // NOTE if block cannot be refactored 'because it wouldn't inline
278
- // then this whole function!
279
- if err != nil {
280
- er := fmt .Errorf (fs + handler .WrapError , append (a [1 :], err )... )
281
- panic (er )
285
+ func T2 [T , U any ](v T , u U , err error ) func (fs string ) (T , U ) {
286
+ return func (fs string ) (T , U ) {
287
+ if err == nil {
288
+ return v , u
282
289
}
283
- return v , u
290
+ panic ( annotateErr ( err , fs ))
284
291
}
292
+
293
+ }
294
+
295
+ func annotateErr (err error , fs string ) error {
296
+ return fmt .Errorf (fs + handler .WrapError , err )
285
297
}
286
298
287
299
// T3 is similar as [To3] but it let's you to annotate a possible error at place.
288
- func T3 [T , U , V any ](v1 T , v2 U , v3 V , err error ) func (fs string , a ... any ) (T , U , V ) {
289
- return func (fs string , a ... any ) (T , U , V ) {
290
- // NOTE if block cannot be refactored 'because it wouldn't inline
291
- // then this whole function!
292
- if err != nil {
293
- er := fmt .Errorf (fs + handler .WrapError , append (a [1 :], err )... )
294
- panic (er )
300
+ func T3 [T , U , V any ](v1 T , v2 U , v3 V , err error ) func (fs string ) (T , U , V ) {
301
+ return func (fs string ) (T , U , V ) {
302
+ if err == nil {
303
+ return v1 , v2 , v3
295
304
}
296
- return v1 , v2 , v3
305
+ panic ( annotateErr ( err , fs ))
297
306
}
298
307
}
0 commit comments