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

  • Home
  • SEARCH
  • 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 8093857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:36:06+00:00 2026-06-05T20:36:06+00:00

I am practicing at getting consistent with my error handling, and I keep hoping

  • 0

I am practicing at getting consistent with my error handling, and I keep hoping to see the code that I’ve written start shrinking. But I built up a domain-meaningful persistence function, and the amount of code I had to write just to do monad handling and custom error handling is astounding.

  • For “programming errors”, I just call error "assertion blown"
  • For really mundane things, I return Nothing (the requested object doesn’t exist)
  • For errors that should be handled, I’m returning Either E V or its equivalent by creating a Control.Monad.Error instance to handle it.

I have in my application multiple functions which I would call primitives, but they can catch certain errors and will raise them my throwing a value of the DBError type. So, I’ve defined them like so:

data DBError = ConversionError ConvertError
         | SaveError String
         | OtherError String 
         deriving (Show, Eq)

instance Error DBError where
    noMsg = OtherError "No message found"
    strMsg s = OtherError s

type DBMonad = ErrorT DBError IO

selectWorkoutByID :: IConnection a => UUID -> a -> DBMonad (Maybe SetRepWorkout)
insertWorkout :: IConnection a => SetRepWorkout -> a -> DBMonad ()

At the level of the calling application, a Workout is a unique object persisted to the database, so the application only ever calls saveWorkout, which itself uses selectWorkoutByID, insertWorkout, and updateWorkout in the ways you would expect:

saveWorkout :: IConnection a => SetRepWorkout -> a -> DBMonad ()
saveWorkout workout conn =
    r <- liftIO $ withTransaction conn $ \conn -> runErrorT $ do
        w_res <- selectWorkoutByID (uuid workout) conn
        case w_res of
            Just w -> updateWorkout workout conn >> return ()
            Nothing -> insertWorkout workout conn >> return ()
    case r of
        Right _ -> return ()
        Left err -> throwError err

This is ugly. I have to run and unwrap a DBMonad, run that in the IO monad, lift the IO back up into the DBMonad, and then check the results and re-wrap the results in the DBMonad.

How can I do this with less, and easier to read, code?

I’m expecting that using my custom application monad to handle recoverable errors would help me to reduce the amount of code I have to write, but this is doing the opposite!

Here are some additional questions:

  • Is there a better way to build up application-semantic errors?
  • Should I be using Control.Exception instead?
  • 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-06-05T20:36:07+00:00Added an answer on June 5, 2026 at 8:36 pm

    After reviewing http://en.wikibooks.org/wiki/Haskell/Monad_transformers, which is the first document on Monad Transformers that really helped me understand them, I figured out a decent solution.

    A new version of the saveWorkout function would look like this:

    saveWorkout :: IConnection a => SetRepWorkout -> a -> DBMonad ()
    saveWorkout workout conn =
        ErrorT $ liftIO $ withTransaction conn $ \conn -> runErrorT $ do
            w_res <- selectWorkoutByID (uuid workout) conn
            case w_res of
                Just w -> updateWorkout workout conn >> return ()
                Nothing -> insertWorkout workout conn >> return ()
    

    The deal is this:

    withTransaction is returning IO Either DBError (). liftIO has the type MonadIO m => IO a -> m a. ErrorT is the standard constructor for everything of the ErrorT monad, and I defined DBMonad to be of that monad. So, I am working with these types:

    withTransaction conn $ <bunch of code> :: IO (Either DBError ())
    liftIO :: MonadIO m => IO (Either DBError ()) -> m (Either DBError ())
    ErrorT :: IO (Either DBError ()) -> ErrorT IO DBError ()
    

    Ideally, since ErrorT/DBMonad are part of the MonadTrans class, I would use simply lift in order to lift IO (Either DBError ()) back up into the ErrorT monad, but at this time I cannot get it to actually type check correctly. This solution, however, still makes the code better by removing the redundent re-wrapping that I had before.

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

Sidebar

Related Questions

Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
While practicing a tutorial on GTK+ I have encountered sample code that looks like
while practicing recursive functions, I have written this code for fibonacci series and (factorial)
So I was practicing some linked list problems and I keep getting mixed up
I am practicing g++ to compile my code yet the error malloc was not
gcc I am just getting back into c programming and I am just practicing
Started practicing with XML and C# and I have an error message of There
I am practicing this https://developer.mozilla.org/en/Canvas_tutorial/Basic_animations Clock tutorial. Everything in the code is clear to
I was practicing some programming problems and tried to code the popular reverse words
I'm practicing writing MVC applications. I have a Mastermind game, that I would like

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.