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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:07:46+00:00 2026-06-14T21:07:46+00:00

I have a group of Data.BSON.Document structures that I am digging through, converting each

  • 0

I have a group of Data.BSON.Document structures that I am digging through, converting each one into a User data structure (I defined User). The function to do the unpacking is pretty straightforward:

docToUser :: Document -> Either String User
docToUser u = do
    name <- look "name" u >>= \(String t) -> return $ unpack t
    email <- look "email" u >>= \(String t) -> return $ unpack t
    token <- look "auth" u >>= \(String t) -> return $ unpack t
    Right $ User name email token

The catch, though, is that it does not actually seem to error out in an Either context. Here are some example runs:

*DB> docToUser ["name" =: "Savanni", "email" =: "savanni@nowhere.com", "auth" =: "random_token"]
Right (User {name = "Savanni", email = "savanni@nowhere.com", token = "random_token"})
*DB> docToUser ["name" =: "Savanni", "email" =: "savanni@nowhere.com", "a" =: "random_token"]
*** Exception: expected "auth" in [ name: "Savanni", email: "savanni@nowhere.com", a: "random_token"]

So, the first run returns a user wrapped inside a Right constructor. The second one I expected something such as Left "field not found", but instead get a full exception. Why is this happening instead of an error stored within an Either data structure?

  • 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-14T21:07:48+00:00Added an answer on June 14, 2026 at 9:07 pm

    Based on information that I can find from here, here, and general other googling around… the instance for the Either monad does not have a fail implementation. On a guess, that would be why I get an exception instead of Left. I wrote this little test to demonstrate:

    eitherMonad :: String -> Either String String
    eitherMonad val = do
        if val == "abcd"
            then fail "val is abcd"
            else return "val is something else"
    
    *DB> eitherMonad "abcd"
    *** Exception: val is abcd
    *DB> eitherMonad "efgh"
    Right "val is something else"
    

    On the other hand, fail :: String -> Maybe String really does return Nothing. It would appear that the correct way to do my docToUser conversion is something more akin to this:

    docToUser :: Document -> Either String User
    docToUser u = do
        let name  = look "name" u :: Maybe Value
        let email = look "email" u :: Maybe Value
        let token = look "auth" u :: Maybe Value
        case (name, email, token) of
            (Just (String n), Just (String e), Just (String t)) -> Right $ User (unpack n) (unpack e) (unpack t)
            (Nothing, _, _) -> Left "username not found"
            (Just (String n), Nothing, _) -> Left "email not found"
            (Just (String n), Just (String e), Nothing) -> Left "auth token not found"
            otherwise -> Left "Something else broke"
    

    I would imagine that can take quite a bit of refinement, especially in detecting and reporting which fields failed. But, this seems to be pretty close to the answer.

    I think, given that, that this question is a duplicate of Is there no standard (Either a) monad instance?

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

Sidebar

Related Questions

I have an algorithm that can group data into a hierarchical cluster tree. The
I have to group some data into categories, based on the column qualifier that
Possible Duplicate: Linq - Top value form each group I have data in the
I have a query that returns data with group category and some details like
I have some data that has various attributes and I want to hierarchically group
Say I have a Group data structure which contains a list of Element objects,
I have a report which has a list that is used to group data
Hi guys Ive got a problem with item renderer.I have a data group that
I have a series of timestamped data points that I wish to group by
I have about 20 data flow tasks arranged into a group in my control

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.