Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 398
muhammed ikinci Avatar asked Aug 15 '21 14:08

muhammed ikinci


People also ask

Should error messages have punctuation?

Use a period in error message text, even if there is only one sentence. If there are multiple sentences, use only one space after the periods, not two.

How do you write an error message?

A good error message has three parts: problem identification, cause details if helpful, and a solution if possible. Whenever an error occurs, user wants to fix it as soon as possible. The error message should have enough information for user that guides him how to get out of the erroneous situation.

What does an error message say?

Be concise If something has failed, an error message should be a short, concise piece of text that tells them what went wrong and provides a potential resolution to the problem. So much text!

Should error messages be capitalized or punctuated?

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. 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.

Where are punctuation errors most likely to occur?

Common Punctuation Errors. Places you’re likely to make a mistake: 1. At the end of a sentence 2. When you have quotations, citations, or parentheses 3. When you haven’t memorized the rules 4. When you need to make a judgment call.

Why is it important to check your writing for punctuation mistakes?

It’s always best to check your writing for common punctuation mistakes so you don’t inadvertently alienate those who will be reading your material. Here are 10 of the most common punctuation errors people make and how you can avoid making them.

Where do you put the punctuation in a quote?

How to Avoid: The punctuation is part of the text you’re quoting, so the punctuation goes inside the quotation marks. Note that in American English, the punctuation goes inside the quotation marks, while the British punctuate outside of the quotation marks.


1 Answers

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

like image 146
blackgreen Avatar answered Oct 20 '22 20:10

blackgreen