Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavlo Strokov <pstrokov@gitlab.com>2020-12-04 10:37:19 +0300
committerPavlo Strokov <pstrokov@gitlab.com>2020-12-04 10:37:19 +0300
commit037a9ddab8a914583c12804ff5c855bef349472b (patch)
treeda1bad5cc1276bcc242cbabb42e959515c079563 /STYLE.md
parent65cef6e601f996fbbeaed5332dda5719b9021d3c (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.md12
1 files changed, 7 insertions, 5 deletions
diff --git a/STYLE.md b/STYLE.md
index f8fde112c..67ba8d4d2 100644
--- a/STYLE.md
+++ b/STYLE.md
@@ -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