Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:33:13+00:00 2026-05-27T03:33:13+00:00

I am struggling with the best way to report errors in a set of

  • 0

I am struggling with the best way to report errors in a set of functions that should compose nicely, in a library I’m working on.

Concretely, I have functions that look like:

foo, bar, baz :: a -> Maybe a

where foo can fail in only one way (a good fit for Maybe), but bar and baz can fail in two different ways each (good fits for Either BarErrors and Either BazErrors).

One solution is to create:

data AllTheErrors = TheFooError
                  | BarOutOfBeer
                  | BarBurnedDown
                  | ...

and make all the functions return Either AllTheErrors, which expresses the range of errors that might be raised by a composed sequence of these functions at the expense of expressing the range of errors possible for each individual function.

Is there a way I can get both? Maybe with something other than monadic composition? Or with type families (waves hands)…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:33:13+00:00Added an answer on May 27, 2026 at 3:33 am

    The Control.Monad.Exception library allows strongly typed exceptions to be used in non IO code. This allows functions to throw errors, and easily compose with functions that throw different errors. For example:

    {-# LANGUAGE RankNTypes, MultiParamTypeClasses, FunctionalDependencies #-}
    {-# LANGUAGE FlexibleInstances #-}
    import Prelude hiding (catch)
    import Control.Monad.Exception
    
    
    data FooException = FooException deriving (Show, Typeable)
    instance Exception FooException
    
    data BarErrors = BarErrors deriving (Show, Typeable)
    instance Exception BarErrors
    
    data BazErrors = BazErrors deriving (Show, Typeable)
    instance Exception BazErrors
    
    -- sample functions    
    foo :: (Throws FooException l) => a -> EM l a
    foo a = return a
    
    
    bar :: (Throws BarErrors l) => a -> EM l a
    bar _ = throw BarErrors
    
    baz :: (Throws BazErrors l) => a -> EM l a
    baz a = return a
    
    
    -- using all at once:
    
    allAtOnce :: (Throws FooException l, Throws BarErrors l, Throws BazErrors l) =>
                 a -> EM l String
    allAtOnce x = do
      _ <- foo x
      _ <- bar x
      _ <- baz x
      return "success!"
    
    -- now running the code, catching the exceptions:
    
    run :: a -> String
    run x = runEM $ allAtOnce x `catch` (\(_ :: FooException) -> return "foo failed")
            `catch` (\BarErrors -> return "bar failed")
            `catch` (\BazErrors -> return "baz failed")
    
    
    -- run 3 results in "bar failed"
    

    See also the papers Explicitly Typed Exceptions for Haskell and An Extensible Dynamically-Typed Hierarchy of Exceptions for more details on using this library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with the best way to make sure that the certain
I love KnockoutJS but have been struggling to figure out the best way to
I have been struggling with the best way of creating, accessing and updating values
I am struggling with the best way to handle a WPF application that has
I'm struggling a bit figuring out the best way accept and store a url
I'm struggling to work out how the best way to store n+1 object in
I'm struggling right now to figure out the best way to get the graphics
I'm new to WPF and MVVM. I'm struggling to determine the best way to
I'm struggling to find the best way to implement my update to the database
Really like objectify, although still struggling with what is the best way to structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.