diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2020-12-04 10:37:19 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2020-12-04 10:37:19 +0300 |
commit | 037a9ddab8a914583c12804ff5c855bef349472b (patch) | |
tree | da1bad5cc1276bcc242cbabb42e959515c079563 /STYLE.md | |
parent | 65cef6e601f996fbbeaed5332dda5719b9021d3c (diff) |
Use %w to wrap errors
Code style document updated to notice on the error wrapping and useful links for reference.
Diffstat (limited to 'STYLE.md')
-rw-r--r-- | STYLE.md | 12 |
1 files changed, 7 insertions, 5 deletions
@@ -10,11 +10,13 @@ code is accessible to different developers with varying setups. ## Errors -### Use %v when wrapping errors +### Use %w when wrapping errors -Use `%v` when wrapping errors with context. +Use `%w` when wrapping errors with context. - fmt.Errorf("foo context: %v", err) + fmt.Errorf("foo context: %w", err) + +It allows to inspect the wrapped error by the caller with [`errors.As`](https://golang.org/pkg/errors/#As) and [`errors.Is`](https://golang.org/pkg/errors/#Is). More info about `errors` package capabilities could be found in the [blog post](https://blog.golang.org/go1.13-errors). ### Keep errors short @@ -23,10 +25,10 @@ them. To be a good neighbor to the rest of the call stack we should keep our errors short. // Good - fmt.Errorf("peek diff line: %v", err) + fmt.Errorf("peek diff line: %w", err) // Too long - fmt.Errorf("ParseDiffOutput: Unexpected error while peeking: %v", err) + fmt.Errorf("ParseDiffOutput: Unexpected error while peeking: %w", err) ### Use lower case in errors |