Search code examples
goerror-handlingstatic-analysis

Why error messages shouldn't end with a punctuation mark in Go?


I have a problem with error text.

If I use the error message shown below, the editor's linter gives a warning like this: "error strings should not end with punctuation or a newline":

return errors.New("Test!")
                       ^

The question is: why shouldn't I use punctuation? What is the real reason behind it?


Solution

  • Errors may be wrapped up the call stack and their messages may be concatenated before printing. If you add punctuation, the end result might be weird from a grammatical standpoint.

    As the link in the comment says:

    Error strings should not be capitalized (unless beginning with proper nouns or acronyms) or end with punctuation, since they are usually printed following other context.

    As an example, consider the following program:

    func main() {
        fmt.Println(foo()) // bar failed: baz failed!: some problem
    }
    
    func foo() error {
        err := bar()
        if err != nil {
            return fmt.Errorf("%s: %w", "bar failed", err)
        }
        return nil
    }
    
    func bar() error {
        err := baz()
        if err != nil {
            return fmt.Errorf("%s: %w", "baz failed!", err)
        }
        return nil
    }
    
    func baz() error {
        return errors.New("some problem")
    }
    

    Of course this example is contrived, but the point is that in a real-world scenario you don't know how your libraries — or users of your code — will format their errors. The issue is probably easier to demonstrate with the error package "github.com/pkg/errors", where each call to Wrap results in a colon (:) separator when printing the message:

    package main
    
    import (
        "github.com/pkg/errors"
        "fmt"
    )
    
    func main() {
        fmt.Println(errors.Wrap(errors.Wrap(errors.Wrap(errors.New("foo"), "bar"), "baz"), "quux"))
        // quux: baz: bar: foo 
    }
    

    Playground: https://play.golang.org/p/dxI2301IX1P