Commit 8b7a31f

mo khan <mo@mokhan.ca>
2025-07-23 17:38:02
feat: add x.Try() to ignore errors and continue
1 parent ded39aa
Changed files (2)
pkg/x/error.go
@@ -14,3 +14,7 @@ func Must[T any](item T, err error) T {
 	Check(err)
 	return item
 }
+
+func Try[T any](item T, _err error) T {
+	return item
+}
pkg/x/error_test.go
@@ -34,3 +34,18 @@ func TestMust(t *testing.T) {
 		})
 	})
 }
+
+func TestTry(t *testing.T) {
+	t.Run("does not panic", func(t *testing.T) {
+		assert.NotPanics(t, func() {
+			Try(0, errors.New("Ooops..."))
+		})
+	})
+
+	t.Run("returns the value", func(t *testing.T) {
+		assert.NotPanics(t, func() {
+			assert.Equal(t, 42, Try(42, nil))
+			assert.Equal(t, "hello", Try("hello", errors.New("Error")))
+		})
+	})
+}