Skip to content

Commit 34754a3

Browse files
committed
Merge branch 'internal-variadic-removal' into repare-major-release
2 parents 7087dd6 + 6a2ea35 commit 34754a3

File tree

5 files changed

+27
-42
lines changed

5 files changed

+27
-42
lines changed

err2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func Handle(err *error, a ...any) {
8686
// how how it works with defer.
8787
r := recover()
8888

89-
if !handler.WorkToDo(r, err) && !handler.NoerrCallToDo(a...) {
89+
if !handler.WorkToDo(r, err) && !handler.NoerrCallToDo(a) {
9090
return
9191
}
9292

@@ -96,7 +96,7 @@ func Handle(err *error, a ...any) {
9696
*err = handler.PreProcess(err, &handler.Info{
9797
CallerName: "Handle",
9898
Any: r,
99-
}, a...)
99+
}, a)
100100
}
101101

102102
// Catch is a convenient helper to those functions that doesn't return errors.
@@ -157,7 +157,7 @@ func Catch(a ...any) {
157157
err = handler.PreProcess(&err, &handler.Info{
158158
CallerName: "Catch",
159159
Any: r,
160-
}, a...)
160+
}, a)
161161
doTrace(err)
162162
}
163163

internal/handler/handler.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ func WorkToDo(r any, err *error) bool {
221221
return (err != nil && *err != nil) || r != nil
222222
}
223223

224-
func NoerrCallToDo(a ...any) (yes bool) {
225-
//var yes bool
224+
// NoerrCallToDo returns if we have the _exception case_, aka, func (noerr bool)
225+
// where these handlers are called even normally only error handlers are called,
226+
// i.e. those which have error to handle.
227+
func NoerrCallToDo(a []any) (yes bool) {
226228
if len(a) != 0 {
227229
_, yes = a[0].(CheckHandler)
228230
}
@@ -249,7 +251,7 @@ func Process(info *Info) {
249251

250252
// PreProcess is currently used for err2 API like err2.Handle and .Catch.
251253
// - replaces the Process
252-
func PreProcess(errPtr *error, info *Info, a ...any) error {
254+
func PreProcess(errPtr *error, info *Info, a []any) error {
253255
// Bug in Go?
254256
// start to use local error ptr only for optimization reasons.
255257
// We get 3x faster defer handlers without unsing ptr to original err
@@ -265,7 +267,7 @@ func PreProcess(errPtr *error, info *Info, a ...any) error {
265267
const lvl = -1
266268

267269
if len(a) > 0 {
268-
subProcess(info, a...)
270+
subProcess(info, a)
269271
} else {
270272
fnName := "Handle" // default
271273
if info.CallerName != "" {
@@ -307,16 +309,7 @@ func PreProcess(errPtr *error, info *Info, a ...any) error {
307309
return err
308310
}
309311

310-
// firstArgIsString not used any more.
311-
func _(a ...any) bool {
312-
if len(a) > 0 {
313-
_, isStr := a[0].(string)
314-
return isStr
315-
}
316-
return false
317-
}
318-
319-
func subProcess(info *Info, a ...any) {
312+
func subProcess(info *Info, a []any) {
320313
// not that switch cannot be 0: see call side
321314
switch len(a) {
322315
case 0:
@@ -325,22 +318,22 @@ programming error: subProcess: case 0:
325318
---`
326319
fmt.Fprintln(os.Stderr, color.Red()+msg+color.Reset())
327320
case 1:
328-
processArg(info, 0, a...)
321+
processArg(info, 0, a)
329322
default: // case 2, 3, ...
330-
processArg(info, 0, a...)
323+
processArg(info, 0, a)
331324
if _, ok := a[1].(PanicFn); ok {
332-
processArg(info, 1, a...)
325+
processArg(info, 1, a)
333326
} else if _, ok := a[1].(ErrorFn); ok {
334327
// check second ^ and then change the rest by combining them to
335328
// one that we set to proper places: ErrorFn and NilFn
336-
hfn := Pipeline(AssertErrHandlers(a))
329+
hfn := Pipeline(ToErrorFns(a))
337330
info.ErrorFn = hfn
338331
info.NilFn = hfn
339332
}
340333
}
341334
}
342335

343-
func processArg(info *Info, i int, a ...any) {
336+
func processArg(info *Info, i int, a []any) {
344337
switch first := a[i].(type) {
345338
case string:
346339
info.Format = first
@@ -411,25 +404,17 @@ func Pipeline(f []ErrorFn) ErrorFn {
411404
}
412405
}
413406

414-
func bugAssertErrHandlers(handlerFns ...any) (hs []ErrorFn) {
415-
hs = make([]ErrorFn, 0, len(handlerFns))
416-
for _, a := range handlerFns {
417-
if fn, ok := a.(ErrorFn); ok {
418-
hs = append(hs, fn)
419-
} else {
420-
return nil
421-
}
422-
}
423-
return hs
424-
}
425-
426-
func AssertErrHandlers(handlerFns []any) (hs []ErrorFn) {
407+
func ToErrorFns(handlerFns []any) (hs []ErrorFn) {
427408
count := len(handlerFns)
428409
hs = make([]ErrorFn, 0, count)
429410
for _, a := range handlerFns {
430411
if fn, ok := a.(ErrorFn); ok {
431412
hs = append(hs, fn)
432413
} else {
414+
msg := `---
415+
assertion vialation: your handlers should be 'func(erro) error' type
416+
---`
417+
fmt.Fprintln(os.Stderr, color.Red()+msg+color.Reset())
433418
return nil
434419
}
435420
}

internal/handler/handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ var Info = handler.Info{
141141
func Handle() {
142142
a := []any{}
143143
Info.Err = &myErrVal
144-
myErrVal = handler.PreProcess(&myErrVal, &Info, a...)
144+
myErrVal = handler.PreProcess(&myErrVal, &Info, a)
145145
}
146146

147147
func TestPreProcess_debug(t *testing.T) {
@@ -241,7 +241,7 @@ func TestPreProcess(t *testing.T) {
241241
var err = x.Whom(tt.args.Info.Err != nil,
242242
*tt.args.Info.Err, nil)
243243

244-
err = handler.PreProcess(&err, &tt.args.Info, tt.args.a...)
244+
err = handler.PreProcess(&err, &tt.args.Info, tt.args.a)
245245

246246
test.RequireEqual(t, panicHandlerCalled, tt.want.panicCalled)
247247
test.RequireEqual(t, errorHandlerCalled, tt.want.errorCalled)

internal/handler/handlers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestHandlers(t *testing.T) {
3838
anys := tt.args.f
3939

4040
test.Require(t, anys != nil, "cannot be nil")
41-
fns := handler.AssertErrHandlers(anys)
41+
fns := handler.ToErrorFns(anys)
4242
test.Require(t, fns != nil, "cannot be nil")
4343

4444
errHandler := handler.Pipeline(fns)

try/out.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type (
4343
//
4444
// error sending response: UDP not listening
4545
func (o *Result) Logf(a ...any) *Result {
46-
return o.logf(logfFrameLvl, a...)
46+
return o.logf(logfFrameLvl, a)
4747
}
4848

4949
// Logf prints a log line to pre-set logging stream (err2.SetLogWriter)
@@ -57,7 +57,7 @@ func (o *Result) Logf(a ...any) *Result {
5757
//
5858
// error sending response: UDP not listening
5959
func (o *Result1[T]) Logf(a ...any) *Result1[T] {
60-
o.Result.logf(logfFrameLvl, a...)
60+
o.Result.logf(logfFrameLvl, a)
6161
return o
6262
}
6363

@@ -72,7 +72,7 @@ func (o *Result1[T]) Logf(a ...any) *Result1[T] {
7272
//
7373
// error sending response: UDP not listening
7474
func (o *Result2[T, U]) Logf(a ...any) *Result2[T, U] {
75-
o.Result.logf(logfFrameLvl, a...)
75+
o.Result.logf(logfFrameLvl, a)
7676
return o
7777
}
7878

@@ -262,7 +262,7 @@ func wrapStr() string {
262262
return ": %w"
263263
}
264264

265-
func (o *Result) logf(lvl int, a ...any) *Result {
265+
func (o *Result) logf(lvl int, a []any) *Result {
266266
if o.Err == nil {
267267
return o
268268
}

0 commit comments

Comments
 (0)