aboutsummaryrefslogtreecommitdiff
path: root/internal/errext
diff options
context:
space:
mode:
authorStefan Majewsky <stefan.majewsky@sap.com>2023-10-25 13:49:14 +0200
committerGitHub <noreply@github.com>2023-10-25 13:49:14 +0200
commitc48895200c34150e8ed0e6c726d6123310e88804 (patch)
tree3ae8d6b34cbed323d0936503ecd579695c762249 /internal/errext
parent1bc6c5a6fcf5bf0f52a960b6df87b6bd04e10956 (diff)
parent6f2956376fa67c4fd3abbed65d1ec20bea2b9e00 (diff)
downloadgo-schwift-c48895200c34150e8ed0e6c726d6123310e88804.tar.gz
Merge pull request #16 from SuperSandro2000/fix-lints
Fix remaining lints
Diffstat (limited to 'internal/errext')
-rw-r--r--internal/errext/errext.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/internal/errext/errext.go b/internal/errext/errext.go
new file mode 100644
index 0000000..3655ba7
--- /dev/null
+++ b/internal/errext/errext.go
@@ -0,0 +1,26 @@
+package errext
+
+import "errors"
+
+// vendored from https://github.com/sapcc/go-bits/blob/master/errext/errext.go (also licensed Apache 2.0) to prevent go.mod go bump to 1.21
+
+// As is a variant of errors.As() that leverages generics to present a nicer interface.
+//
+// //this code:
+// var perr os.PathError
+// if errors.As(err, &perr) {
+// handle(perr)
+// }
+// //can be rewritten as:
+// if perr, ok := errext.As[os.PathError](err); ok {
+// handle(perr)
+// }
+//
+// This is sometimes more verbose (like in this example), but allows to scope
+// the specific error variable to the condition's then-branch, and also looks
+// more idiomatic to developers already familiar with type casts.
+func As[T error](err error) (T, bool) {
+ var result T
+ ok := errors.As(err, &result)
+ return result, ok
+}