Files
coding-standards/components/errors.md
T
2026-11-14 19:16:24 +05:00

1009 B

Errors

Errors are handled using standard error type.

When neccessary (e.g. returning back to caller from another package) fmt.Errorf should be used.

General error syntax is:

$NAME $TYPE: %w

Where:

  • $NAME is a type's name.
  • $TYPE is a type as string.

Do not try to return dynamically formatted error. If error's text (or part of it) is static - put it into a variable, and if that static text appears more than once in different functions - put it into unexported package variable.

Errors can be set as exported variable only if it supposed to be checked (or actually checked) in another package.

Good example:

package a

import "errors"

var errA = errors.New("a")

func someFunc() error {
    if err := otherFunc(); err != nil {
        return fmt.Errorf("%w: someFunc: %w", errA, err)
    }

    return nil
}

Bad example:

package a

import errors

func someFunc() error {
    if err := otherFunc(); err != nil {
        return err
    }

    return nil
}