solo-io / go-utils

golang utilities
Apache License 2.0
110 stars 18 forks source link

Fail handler reports incorrect line numbers #443

Closed marcogschmidt closed 3 years ago

marcogschmidt commented 3 years ago

We need to account for the extra function in the call stack when registering a custom fail handler, otherwise the line numbers that are reported on failures are incorrect, e.g.: image

The solution is this:

RegisterFailHandler(func(message string, callerSkip ...int) {
        // Your custom logic here...
​
        // Account for this extra function in the call stack.
        // Without this all failure messages will show the incorrect line number!
        var shiftedCallerSkip []int
        for _, i := range callerSkip {
            shiftedCallerSkip = append(shiftedCallerSkip, i+1)
        }
​
        Fail(message, shiftedCallerSkip...)
    })