haskell facebook异常 [英] haskell facebook exceptions

查看:153
本文介绍了haskell facebook异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读后,我尝试了

{-# LANGUAGE OverloadedStrings, NoMonomorphismRestriction #-}
module Login (
    fbUrl,
    fbEmail
) where

-- package http://hackage.haskell.org/package/fb
import qualified Facebook as FB
import Network.HTTP.Conduit (withManager)

app :: FB.Credentials
app = FB.Credentials "localhost" "249348058430770" "..."

url :: FB.RedirectUrl
url = "http://localhost/fb"

perms :: [FB.Permission]
perms = ["user_about_me", "email"]

--fbUrl :: Monad m => FB.FacebookT FB.Auth m Text
fbUrl :: IO Text
fbUrl = withManager $ \manager -> FB.runFacebookT app manager $ FB.getUserAccessTokenStep1 url perms

--fbEmail :: Monad m => (ByteString, ByteString) -> FB.FacebookT FB.Auth m (Maybe Text)
--fbEmail :: (ByteString, ByteString) -> IO (Maybe Text)
fbEmail c = withManager $ \manager -> FB.runFacebookT app manager $ do
    t <- FB.getUserAccessTokenStep2 url [c]
    u <- FB.getUser "me" [] (Just t)
    return $ FB.userEmail u

module Main (
  main
) where

import Login
import qualified Data.ByteString.Char8 as C
import Control.Exception

main :: IO ()
main = do

    let a = ("code","test")
    e <- fbEmail $ (\(x,y) -> (C.pack x, C.pack y)) a

    case e of
        Nothing -> print "doh!"
        Just e -> print e

我得到 haskell-facebook:FacebookException {fbeType =invalid_code ,fbeMessage =验证码格式无效。} 而不是 doh!

使用 e < - try(fbEmail $(\(x,y) - >(C.pack x,C.pack y))a) / p>

With e <- try (fbEmail $ (\(x,y) -> (C.pack x, C.pack y)) a) i get

Couldn't match expected type `Either
                                e0 (Maybe Data.Text.Internal.Text)'
            with actual type `Maybe t0'
In the pattern: Nothing
In a case alternative: Nothing -> print "doh!"
In a stmt of a 'do' block:
  case e of {
    Nothing -> print "doh!"
    Just e -> print e }

@Daniel Fischer

@Daniel Fischer

let a = ("code","test")
e <- try (fbEmail $ (\(x,y) -> (C.pack x, C.pack y)) a)

case e of
    Left x -> print "doh!"
    Right e -> print "ok"

Ambiguous type variable `e0' in the constraint:
  (Exception e0) arising from a use of `try'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' block:
  e <- try (fbEmail $ (\ (x, y) -> (C.pack x, C.pack y)) a)
In the expression:
  do { let a = ...;
       e <- try (fbEmail $ (\ (x, y) -> (C.pack x, C.pack y)) a);
       case e of {
         Left x -> print "doh!"
         Right e -> print "ok" } }
In an equation for `main':
    main
      = do { let a = ...;
             e <- try (fbEmail $ (\ (x, y) -> (C.pack x, C.pack y)) a);
             case e of {
               Left x -> print "doh!"
               Right e -> print "ok" } }

当我添加一个类型签名 fbEmail :: Monad m => (ByteString,ByteString) - > FB.FacebookT FB.Auth m(可能文本)我得到

When I add a type signature fbEmail :: Monad m => (ByteString, ByteString) -> FB.FacebookT FB.Auth m (Maybe Text) I get

Could not deduce (monad-control-0.3.1.3:Control.Monad.Trans.Control.MonadBaseControl
                    IO m,
                  resourcet-0.3.2.2:Control.Monad.Trans.Resource.MonadUnsafeIO m,
                  Control.Monad.IO.Class.MonadIO m,
                  resourcet-0.3.2.2:Control.Monad.Trans.Resource.MonadThrow
                    (FB.FacebookT FB.Auth m))
  arising from a use of `withManager'
from the context (Monad m)
  bound by the type signature for
             fbEmail :: Monad m =>
                        (ByteString, ByteString) -> FB.FacebookT FB.Auth m (Maybe Text)
  at src/Login.hs:(25,1)-(28,27)
Possible fix:
  add (monad-control-0.3.1.3:Control.Monad.Trans.Control.MonadBaseControl
         IO m,
       resourcet-0.3.2.2:Control.Monad.Trans.Resource.MonadUnsafeIO m,
       Control.Monad.IO.Class.MonadIO m,
       resourcet-0.3.2.2:Control.Monad.Trans.Resource.MonadThrow
         (FB.FacebookT FB.Auth m)) to the context of
    the type signature for
      fbEmail :: Monad m =>
                 (ByteString, ByteString) -> FB.FacebookT FB.Auth m (Maybe Text)
  or add instance declarations for
     (monad-control-0.3.1.3:Control.Monad.Trans.Control.MonadBaseControl
        IO m,
      resourcet-0.3.2.2:Control.Monad.Trans.Resource.MonadThrow
        (FB.FacebookT FB.Auth m))
In the expression: withManager
In the expression:
  withManager
  $ \ manager
      -> FB.runFacebookT app manager
         $ do { t <- FB.getUserAccessTokenStep2 url [...];
                u <- FB.getUser "me" [] (Just t);
                .... }
In an equation for `fbEmail':
    fbEmail c
      = withManager
        $ \ manager
            -> FB.runFacebookT app manager
               $ do { t <- FB.getUserAccessTokenStep2 url ...;
                      .... }

当我添加 fbEmail :: (ByteString,ByteString) - > IO(可能文本)我得到

Ambiguous type variable `e0' in the constraint:
  (Exception e0) arising from a use of `try'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' block:
  e <- try (fbEmail $ (\ (x, y) -> (C.pack x, C.pack y)) a)
In the expression:
  do { couchTest;
       u <- fbUrl;
       print u;
       let a = ...;
       .... }
In an equation for `main':
    main
      = do { couchTest;
             u <- fbUrl;
             print u;
             .... }


推荐答案

try 在结果的顶部添加另一个图层。为了使这个例子很简单,我通过使用catch-all SomeException 来捕捉所有的异常。

try adds another layer on top of your result. To make this example simple, I'm catching all exceptions by using the catch-all SomeException.

更清楚一点,这里是添加了类型signatured的结果类型:

To make it a bit more clear, here are the result types with type signatured added:

e :: Maybe Text <- fbEmail $ (\(x,y) -> (C.pack x, C.pack y)) a

的使用相比,尝试,异常类型等于 SomeException

e :: Either SomeException (Maybe Text) <- fbEmail $ (\(x,y) -> (C.pack x, C.pack y)) a

使用模式匹配来处理这些更复杂的类型很简单,Daniel提到他的意见:

It's simple to deal with these more complex types using pattern matching as Daniel mentioned in his comments:

{-# LANGUAGE FlexibleContexts, OverloadedStrings #-}
module Main where

import Control.Exception
import qualified Data.ByteString.Char8 as C
import Data.Text
import Network.HTTP.Conduit (withManager)

import qualified Facebook as FB

app :: FB.Credentials
app = FB.Credentials "localhost" "249348058430770" "..."

url :: FB.RedirectUrl
url = "http://localhost/fb"

perms :: [FB.Permission]
perms = ["user_about_me", "email"]

fbUrl :: IO Text
fbUrl = withManager $ \manager -> FB.runFacebookT app manager $ FB.getUserAccessTokenStep1 url perms

fbEmail :: FB.Argument -> IO (Maybe Text)
fbEmail c = withManager $ \manager -> FB.runFacebookT app manager $ do
  t <- FB.getUserAccessTokenStep2 url [c]
  u <- FB.getUser "me" [] (Just t)
  return $ FB.userEmail u

main :: IO ()
main = do
  let a = ("code","test")
  e <- try . fbEmail $ (\(x,y) -> (C.pack x, C.pack y)) a

  case e of
    Left e -> print $ "error: " ++ show (e :: SomeException)
    Right Nothing -> print "doh!"
    Right (Just e) -> print e

这篇关于haskell facebook异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆