Initial commit.
This commit is contained in:
13 files changed
+711
No files matched your search
@@ -0,0 +1,54 @@
|
||||
# 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:
|
||||
|
||||
```go
|
||||
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:
|
||||
|
||||
```go
|
||||
package a
|
||||
|
||||
import errors
|
||||
|
||||
func someFunc() error {
|
||||
if err := otherFunc(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
```
|
||||
Reference in new issue
Block a user