Djarvur / go-err113

Golang linter to check the errors handling expressions
MIT License
45 stars 4 forks source link

Question about errors that are just a string #12

Closed Zamiell closed 4 years ago

Zamiell commented 4 years ago

Greetings and thanks for the linter.

The linter doesn't seem to like the following code:

func testingSubroutine(i1 int, i2 int) error {
    if i1 != i2 {
        return errors.New("i1 and i2 are not equal!")
    }

    return nil
}

It gives the error:

err113: do not define dynamic errors, use wrapped static errors instead

However, this seems nonsensical, because there is nothing to wrap. I'm just creating a brand new error. Is this a bug in the linter or am I misunderstanding the error message?

onokonem commented 4 years ago

Is this a bug in the linter or am I misunderstanding the error message?

Actually, neither

the main idea behind this linter was to force people (mostly the people I'm working with) to build an errors hierarchy.

I would modify your code this way:

var ErrNotEqual = errors.New("not equal")

func testingSubroutine(i1 int, i2 int) error {
    if i1 != i2 {
        return fmt.Errorf("i1 and i2: %w", ErrNotEqual)
    }

    return nil
}

As a result, we can pass the equal info in the error and at the same time the error is analysable: you can check is it non-equal error with errors.Is(err, ErrNotEqual)

Zamiell commented 4 years ago

Excellent, thank you.