git.iko.soy/rowtype-yoga/purescript-yoga-om
on main help sign in
Add conditional typed error helpers
commitad15ddc8549cd941a03b12b477477cbda2442e9e authorMark <mark.eibes@gmail.com> dateTue 14 Jul 2026, 22:32 (3 months ago) parent35377ee
3 files changed+53−1
unifiedsplit
spago.yaml+1−1 src/Yoga/Om.purs+22 test/Test/Main.purs+30
spago.yaml+1−1
@@ -2,7 +2,7 @@ workspace: {}
22package:
33 name: yoga-om
44 publish:
5− version: 2.2.0
5+ version: 2.2.1
66 license: MIT
77 exclude:
88 - "src/**/*.test.purs"
src/Yoga/Om.purs+22
@@ -42,6 +42,8 @@ module Yoga.Om
4242 , tapM
4343 , fatal
4444 , throw
45+ , throwUnless
46+ , throwWhen
4547 , throwLeftAs
4648 , throwLeftAsM
4749 , class ToOm
@@ -130,6 +132,26 @@ throw
130132 → m a
131133throw = throwError <<< error
132134
135+-- | Throw the supplied typed error when the condition is true.
136+throwWhen
137+ ∷ ∀ m err errors
138+ . SingletonVariantRecord err errors
139+ ⇒ MonadThrow (OneOfTheseErrors errors) m
140+ ⇒ Record err
141+ → Boolean
142+ → m Unit
143+throwWhen problem condition = when condition (throw problem)
144+
145+-- | Throw the supplied typed error when the condition is false.
146+throwUnless
147+ ∷ ∀ m err errors
148+ . SingletonVariantRecord err errors
149+ ⇒ MonadThrow (OneOfTheseErrors errors) m
150+ ⇒ Record err
151+ → Boolean
152+ → m Unit
153+throwUnless problem condition = unless condition (throw problem)
154+
133155fatal ∷ ∀ ctx err a. String → Om ctx err a
134156fatal msg = throw { exception: Exn.error msg }
135157
test/Test/Main.purs+30
@@ -246,6 +246,36 @@ main = launchAff_ $ runSpec [ consoleReporter ] do
246246 # liftAff
247247 result `shouldEqual` 99
248248
249+ it "throwWhen throws only when its condition is true" do
250+ let
251+ run condition = do
252+ Om.throwWhen { rejected: "blocked" } condition
253+ pure "continued"
254+ handle =
255+ Om.runOm {}
256+ { exception: \_ -> pure "exception"
257+ , rejected: pure
258+ }
259+ whenTrue <- run true # handle # liftAff
260+ whenFalse <- run false # handle # liftAff
261+ whenTrue `shouldEqual` "blocked"
262+ whenFalse `shouldEqual` "continued"
263+
264+ it "throwUnless throws only when its condition is false" do
265+ let
266+ run condition = do
267+ Om.throwUnless { rejected: "blocked" } condition
268+ pure "continued"
269+ handle =
270+ Om.runOm {}
271+ { exception: \_ -> pure "exception"
272+ , rejected: pure
273+ }
274+ whenTrue <- run true # handle # liftAff
275+ whenFalse <- run false # handle # liftAff
276+ whenTrue `shouldEqual` "continued"
277+ whenFalse `shouldEqual` "blocked"
278+
249279 it "throwLeftAs converts Left to error" do
250280 result <- Left "bad" # Om.throwLeftAs (\badError -> Om.error { badError })
251281 # runExampleOm