checked-exceptions: mtl-style checked exceptions

[ control, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

A monad transformer that allows you to throw and catch a restricted set of exceptions, tracked at the type level.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.2.0.0
Change log CHANGELOG.md
Dependencies base (>=4.12.0.0 && <5), constraints (>=0.14 && <0.15), exceptions (>=0.10 && <0.11), ghc (>=8.6.5 && <10), ghc-tcplugins-extra (>=0.4 && <0.6), mtl (>=2.0.0.0 && <3.0.0.0) [details]
Tested with ghc ==9.10.1
License MIT
Author Zachary Churchill
Maintainer zacharyachurchill@gmail.com
Revised Revision 1 made by goolord at 2025-03-06T19:26:49Z
Category Control
Home page http://github.com/goolord/checked-exceptions
Bug tracker http://github.com/goolord/checked-exceptions/issues
Source repo head: git clone https://github.com/goolord/checked-exceptions.git
Uploaded by goolord at 2025-03-05T18:46:13Z
Distributions
Downloads 13 total (13 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for checked-exceptions-0.2.0.0

[back to package description]

checked-exceptins

A monad transformer that allows you to throw and catch a restricted set of exceptions, tracked at the type level

Consider the following example:

type TestExceptions = '[(), Int, Bool, String]

testCE :: CheckedExceptT TestExceptions IO ()
testCE = CheckedExcept.do
  () <- testCE1 :: CheckedExceptT '[()] IO ()
  () <- testCE2 :: CheckedExceptT '[Int] IO ()
  () <- testCE3 :: CheckedExceptT '[Bool] IO ()
  () <- testCE4 :: CheckedExceptT '[String] IO ()
  -- () <- testCE5 :: CheckedExceptT '[Char] IO () -- doesn't compile
  pure ()

test :: CheckedExcept TestExceptions () -> IO ()
test ce = case runCheckedExcept ce of
  Left e -> do 
    applyAll (putStrLn . encodeException) e
    -- or
    withOneOf @() e $ \() -> putStrLn "()"
    withOneOf @Int e $ \n -> print $ n + 1
    withOneOf @Bool e $ \_ -> pure ()
    -- or
    caseException e
      (  (\() -> putStrLn "()")
      <: (\n -> print $ n + 1)
      <: CaseAny (\x -> putStrLn $ encodeException x)
      -- <: (\b -> putStrLn "bool")
      -- <: (\s -> putStrLn "string")
      -- <: CaseEnd
      )
  Right () -> putStrLn "Right"

The facilities this library provides will alert you when you have, intentionally or unintentionally, introduced a new possible exception in your code that is presently unaccounted for. Since we enforce at the type level what kinds of exceptions are permissible, you can safely trust the exceptions set in the type signature to do something like generate OpenAPI documenation for an HTTP handler's error responses.

When catching an exception, we provide the CaseException type to allow coverage checking with a case-like api caseException, or you can use methods provided by the CheckedException typeclass to perform common operations on exceptions without inspecting the type of the exception.