ubuntu / decorate

Go package containing various helpers to decorate errors with fewer lines of code in functions.
MIT License
3 stars 2 forks source link

fix: deferred LogOnError never logs #20

Closed EduardGomezEscandell closed 7 months ago

EduardGomezEscandell commented 7 months ago

Found a bug in LogOnError: it captures the error at the time defer is called, and not during the return. This means that it'll never log.

This problem managed to sneak through the cracks because the tests are not run in the same way as production code. In production, you always defer the decorate statements. Hence, the fist commit refactors the tests to use defer, and the second commit fixes the issue (THIS BREAKS API).

As an example, this code will never log:

func isEven(x int) (err error) {
    defer decorate.LogOnError(err)

    if x == 0 {
       return true
    } else if x == 1 {
       return false
    } else if x == 2 {
       return true
    } else if x == 3 {
       return false
    } else if x == 4 {
       return true
    } else if x == 5 {
       return false
    }

    return errors.New("number too big")
}