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 7248571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:09:22+00:00 2026-05-28T22:09:22+00:00

Something that happens to me a lot while web programming: I want to run

  • 0

Something that happens to me a lot while web programming: I want to run an operation that has a chance of failure. On a failure, I want to send the client a 500. Normally though, I just want to continue executing a series of steps.

doSomeWebStuff :: SomeWebMonad ()
doSomeWebStuff = do
    res <- databaseCall
    case res of 
        Left err -> status 500
        Right val -> do
             res2 <- anotherDatabaseCall (someprop val)
             case res2 of 
                 Left err -> status 500
                 Right val2 -> text $ show val2

since the errs are exceptions, I don’t like that I need all that case stuff just to catch them. I want to do the same thing whenever anything is a left. Is there a way to express that on one line with something like guard, but control what it returns on an exit?

In another language I could do this:

function doSomeWebStuff() {
    var res = databaseCall()
    if (res == Error) return status 500
    var res2 = anotherDatabaseCall(res.someprop)
    if (res2 == Error) return status 500
    return text(res2)
}

So, I’m ok writing some boilerplate, but I don’t want the errors to mess with my nesting, when it’s far more common to just want to continue forward with the found case.

What’s the cleanest way to do this? I know in theory I can use a monad to exit early on a failure, but I’ve only seen examples with Maybe and it would return Nothing at the end, rather than letting me specify what it returns.

  • 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-28T22:09:23+00:00Added an answer on May 28, 2026 at 10:09 pm

    Here’s how I would do it with ErrorT. Disclaimer: I have never actually used ErrorT before.

    webStuffOr500 :: ErrorT String SomeWebMonad () -> SomeWebMonad ()
    webStuffOr500 action = do
      res <- runErrorT action
      case res of
        Left err -> do
          logError err -- you probably want to know what went wrong
          status 500
        Right () -> return ()
    
    doSomeWebStuff :: SomeWebMonad ()
    doSomeWebStuff = webStuffOr500 doSomeWebStuff'
    
    doSomeWebStuff' :: ErrorT String SomeWebMonad ()
    doSomeWebStuff' = do
        val <- ErrorT databaseCall
        val2 <- ErrorT $ anotherDatabaseCall (someprop val)
        lift $ text $ show val2
    

    Here are the imports and type declarations I used to make sure it all typechecks correctly:

    import Control.Monad.Identity
    import Control.Monad.Error
    import Control.Monad.Trans (lift)
    import Control.Monad
    
    type SomeWebMonad = Identity
    
    data Foo = Foo
    data Bar = Bar
    data Baz = Baz deriving (Show)
    
    someprop :: Foo -> Bar
    someprop = undefined
    databaseCall :: SomeWebMonad (Either String Foo)
    databaseCall = undefined
    anotherDatabaseCall :: Bar -> SomeWebMonad (Either String Baz)
    anotherDatabaseCall = undefined
    logError :: String -> SomeWebMonad ()
    logError = undefined
    text :: String -> SomeWebMonad ()
    text = undefined
    status :: Int -> SomeWebMonad ()
    status = undefined
    

    If I’m doing this all wrong then please, somebody shout out. It may be wise, if you take this approach, to modify the type signature of databaseCall and anotherDatabaseCall to also use ErrorT, that way a <- ErrorT b can be reduced to a <- b in doSomeWebStuff'.

    Since I’m a complete noob at ErrorT, I can’t really do any hand-holding besides “here’s some code, go have some fun”.

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

Sidebar

Related Questions

I want that list, because if something horrible happens, and I'll have to reinstall
Something that has been troubling me for a while: The current wisdom is that
So no doubt that building a domain model is something that I think happens
I have one main parent swf that loads several other swfs. If something happens
Something that has always bugged me is how unpredictable the setTimeout() method in Javascript
Something that I often run into with my users is their desire to aquire
Something that has piqued my interest is Objective-C's BOOL type definition. Why is it
I want to do something that seems like it should be fairly simple, but
Something that would really reload the page or resource, ignoring whatever might be in
Something that I've noticed recently on two different machines is that Apache2 installed via

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.