
I'm trying to play with a Yesod application. I had working code before, but tried stretching myself a bit and adding in some error checking. This code worked: getGoogR = do body <- simpleHttp "http://www.google.com" jsonToRepJson $ object ["response" .= (showDigest $ sha1 body)] This is the new code that is trying to use try (imported from Control.Exception) getGoogR = do body <- try (simpleHttp "http://www.google.com") :: IO (Either SomeException Data.ByteString.Lazy.Internal.ByteString) case body of Left _ -> jsonToRepJson $ object ["response" .= ( show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")] Right val -> jsonToRepJson $ object ["response" .= (showDigest $ sha1 val)] and I get this error: Couldn't match expected type `IO b0' with actual type `GHandler sub0 master0 RepJson' In the expression: jsonToRepJson $ object ["response" .= (show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")] In a case alternative: Left _ -> jsonToRepJson $ object ["response" .= (show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")] In a stmt of a 'do' block: case body of { Left _ -> jsonToRepJson $ object ["response" .= (show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")] Right val -> jsonToRepJson $ object ["response" .= (showDigest $ sha1 val)] I'm sure that my problem has a simple fix, but I just don't know what it is. I will be happy to share any other code if people ask, I'm just giving the short version for brevities sake. Thanks in advance, Bryce

Bryce Verdier
This is the new code that is trying to use try (imported from Control.Exception) getGoogR = do body <- try {- [...] -}
[...]
I'm sure that my problem has a simple fix, but I just don't know what it is. I will be happy to share any other code if people ask, I'm just giving the short version for brevities sake.
The fix is indeed simple. In fact you're just using the wrong 'try', because Yesod handlers aren't IO actions, but actions in a monad that is specific to your application, called Handler. The error message suggests that, although the type is spelled out completely there. What you want is the lifted version of 'try', which you find in the Control.Exception.Lifted module. Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

On 9/26/12 4:53 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: This is the new code that is trying to use try (imported from Control.Exception) getGoogR = do body <- try {- [...] -}
[...]
I'm sure that my problem has a simple fix, but I just don't know what it is. I will be happy to share any other code if people ask, I'm just giving the short version for brevities sake. The fix is indeed simple. In fact you're just using the wrong 'try', because Yesod handlers aren't IO actions, but actions in a monad that is specific to your application, called Handler. The error message suggests that, although the type is spelled out completely there.
What you want is the lifted version of 'try', which you find in the Control.Exception.Lifted module.
Greets, Ertugrul
Thank you for the reply Ertugrul, sadly that didn't seem to help. :( Here are my changes: import qualified Control.Exception.Lifted as L ... body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString) I did the full qualified because the first time I tried your suggestion I just "Control.Exception.Lifted" and I got the same error as I'm sharing below. playhaven.hs:51:19: Couldn't match expected type `IO b0' with actual type `GHandler sub0 master0 RepJson' In the expression: Thanks for being willing to help me with this. Bryce
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Bryce Verdier
body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =) Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =)
Greets, Ertugrul
Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got : body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString) But what I don't understand, is why do I have GHandler PlayHaven PlayHaven (Either SomeException ByteString) instead of IO (Either SomeException ByteString)? Thanks again, Bryce
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Sep 27, 2012 at 10:01:25AM -0700, Bryce Verdier wrote:
On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =)
Greets, Ertugrul
Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got :
body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString)
Do you need a type signature there at all? -Brent

On 9/27/12 10:33 AM, Brent Yorgey wrote:
On Thu, Sep 27, 2012 at 10:01:25AM -0700, Bryce Verdier wrote:
On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =)
Greets, Ertugrul Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got :
body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString) Do you need a type signature there at all?
-Brent That is an awesome question. Originally I tried:
body <- try (simpleHttp "http://www.google.com") and I got this error: playhaven.hs:54:73: Ambiguous type variable `a0' in the constraints: (Show a0) arising from a use of `show' at playhaven.hs:54:73-76 (Exception a0) arising from a use of `try' at playhaven.hs:52:13-15 Probable fix: add a type signature that fixes these type variable(s) So I figured that adding the type signature would help with it. If you know of another way to make this work I would really like to hear about it. Thanks, Bryce
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Sep 27, 2012 at 10:38:31AM -0700, Bryce Verdier wrote:
On 9/27/12 10:33 AM, Brent Yorgey wrote:
On Thu, Sep 27, 2012 at 10:01:25AM -0700, Bryce Verdier wrote:
On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =)
Greets, Ertugrul Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got :
body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString) Do you need a type signature there at all?
-Brent That is an awesome question. Originally I tried:
body <- try (simpleHttp "http://www.google.com")
and I got this error: playhaven.hs:54:73: Ambiguous type variable `a0' in the constraints: (Show a0) arising from a use of `show' at playhaven.hs:54:73-76 (Exception a0) arising from a use of `try' at playhaven.hs:52:13-15 Probable fix: add a type signature that fixes these type variable(s)
So I figured that adding the type signature would help with it. If you know of another way to make this work I would really like to hear about it.
Ah, yes, then you do need a type signature. (Sadly I cannot answer your real question, since I don't know anything about the libraries in question.) -Brent

On Thu, Sep 27, 2012 at 7:38 PM, Bryce Verdier
On 9/27/12 10:33 AM, Brent Yorgey wrote:
On Thu, Sep 27, 2012 at 10:01:25AM -0700, Bryce Verdier wrote:
On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO (Either
L.SomeException Data.ByteString.Lazy.Internal.**ByteString)
Almost right, but your type signature is lying. =)
Greets, Ertugrul
Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got :
body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString)
Do you need a type signature there at all?
-Brent
That is an awesome question. Originally I tried:
body <- try (simpleHttp "http://www.google.com")
this can be made to work by adding less stuff :
getGoogR = do body <- try (simpleHttp "http://www.google.com") case body of Left (SomeException _) -> jsonToRepJson $ object ["response" .= ( show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")] Right val -> jsonToRepJson $ object ["response" .= (showDigest $ sha1 val)]
I think that should work... -- Jedaï

On Thu, Sep 27, 2012 at 9:25 PM, Chaddaï Fouché
body <- try (simpleHttp "http://www.google.com") case body of Left (SomeException _) -> jsonToRepJson $ object ["response" .= ( show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
I think that should work...
Since the signature was made necessary by the ambiguity on which type of exception you wished to catch with your "try", using (SomeException _) in your pattern match should make that clear. By the way, why are you show()ing a String ? The only thing that does is adding quotes around it and escaping some characters, shouldn't you just use :
Left (SomeException _) -> jsonToRepJson $ object ["response" .= "ERROR: NO DATA FOR ONE REASON OR ANOTHER"]
-- Jedaï

On 9/27/12 12:43 PM, Chaddaï Fouché wrote:
On Thu, Sep 27, 2012 at 9:25 PM, Chaddaï Fouché
wrote: body <- try (simpleHttp "http://www.google.com") case body of Left (SomeException _) -> jsonToRepJson $ object ["response" .= ( show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
I think that should work...
Thank you for that Jedai! That looks MUCH MUCH cleaner than the :: GHandler ... type signature.
Since the signature was made necessary by the ambiguity on which type of exception you wished to catch with your "try", using (SomeException _) in your pattern match should make that clear. By the way, why are you show()ing a String ? The only thing that does is adding quotes around it and escaping some characters, shouldn't you just use :
Left (SomeException _) -> jsonToRepJson $ object ["response" .= "ERROR: NO DATA FOR ONE REASON OR ANOTHER"]
I was doing that because when I removed it, I got an error similar to this: Ambiguous type variable `a0' in the constraints: (Data.String.IsString a0) arising from the literal `"ERROR: "' at playhaven.hs:54:73-81 (aeson-0.6.0.2:Data.Aeson.Types.Class.ToJSON a0)
which I was able to remove by throwing a show in there. If you know of a better way I would love to know about it. Thanks again! Bryce

On Thu, Sep 27, 2012 at 11:37 PM, Bryce Verdier
Left (SomeException _) -> jsonToRepJson $ object ["response" .= "ERROR: NO DATA FOR ONE REASON OR ANOTHER"]
I was doing that because when I removed it, I got an error similar to this:
Ambiguous type variable `a0' in the constraints: (Data.String.IsString a0) arising from the literal `"ERROR: "' at playhaven.hs:54:73-81 (aeson-0.6.0.2:Data.Aeson.Types.Class.ToJSON a0)
which I was able to remove by throwing a show in there. If you know of a better way I would love to know about it.
This is probably due to you using OverloadedStrings extension, you can remove the error while avoiding a superfluous show in two ways : either you put an explicit type signature on the string or you make the conversion explicit :
Left (SomeException _) -> jsonToRepJson $ object ["response" .= ("ERROR: NO DATA FOR ONE REASON OR ANOTHER" :: Text)]
(or ByteString or String, whatever)
Left (SomeException _) -> jsonToRepJson $ object ["response" .= (B.pack "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
where B.pack is the ByteString pack (you'll need to import it or import bytestring qualified as B) Both those solutions avoid extra quotes or other annoying quirks of show. -- Jedaï

I think what he is asking is if try (from lifted) has type
try :: (MonadBaseControlhttp://hackage.haskell.org/packages/archive/monad-control/0.3.1.4/doc/html/C...
IOhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/System-IO....m,
Exceptionhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Control-Ex...e)
=> m a -> m (
Eitherhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Data-Eithe...e
a)
and simpleHttp is
simpleHttp "googe" ::
IOhttp://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/System-IO....(
Resulthttp://hackage.haskell.org/packages/archive/HTTP/latest/doc/html/Network-Str...(
Responsehttp://hackage.haskell.org/packages/archive/HTTP/latest/doc/html/Network-HTT...ty))
then how can the whole statement have
try $ simpleHttp "goog" :: GHandler master sub (Either SomeException
ByteString)
when the m in both parts of try is the same, it should end up IO not
GHandler master sub. So I don't understand why this would work, either.
On Thu, Sep 27, 2012 at 1:33 PM, Brent Yorgey
On Thu, Sep 27, 2012 at 10:01:25AM -0700, Bryce Verdier wrote:
On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO (Either L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =)
Greets, Ertugrul
Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got :
body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString)
Do you need a type signature there at all?
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Ignore my message. I think I'm looking at the wrong version of simpleHttp
or something. I should have tried it in ghci before I posted.
On Thu, Sep 27, 2012 at 1:43 PM, David McBride
I think what he is asking is if try (from lifted) has type
try :: (MonadBaseControlhttp://hackage.haskell.org/packages/archive/monad-control/0.3.1.4/doc/html/C... IOhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/System-IO....m, Exceptionhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Control-Ex...e) => m a -> m ( Eitherhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Data-Eithe...e a)
and simpleHttp is
simpleHttp "googe" :: IOhttp://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/System-IO....( Resulthttp://hackage.haskell.org/packages/archive/HTTP/latest/doc/html/Network-Str...( Responsehttp://hackage.haskell.org/packages/archive/HTTP/latest/doc/html/Network-HTT...ty))
then how can the whole statement have
try $ simpleHttp "goog" :: GHandler master sub (Either SomeException ByteString)
when the m in both parts of try is the same, it should end up IO not GHandler master sub. So I don't understand why this would work, either.
On Thu, Sep 27, 2012 at 1:33 PM, Brent Yorgey
wrote: On Thu, Sep 27, 2012 at 10:01:25AM -0700, Bryce Verdier wrote:
On 9/26/12 6:00 PM, Ertugrul Söylemez wrote:
Bryce Verdier
wrote: body <- L.try (simpleHttp "http://www.google.com") :: IO
(Either
L.SomeException Data.ByteString.Lazy.Internal.ByteString) Almost right, but your type signature is lying. =)
Greets, Ertugrul
Thanks for your hint, with a fair amount of head bashing against a wall I was able to figure this out. At the end of day I got :
body <- try (simpleHttp "http://www.google.com") :: GHandler PlayHaven PlayHaven (Either SomeException ByteString)
Do you need a type signature there at all?
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Brent Yorgey
-
Bryce Verdier
-
Chaddaï Fouché
-
David McBride
-
Ertugrul Söylemez