7
7
TypeSynonymInstances,
8
8
OverloadedStrings #-}
9
9
10
- #if MIN_VERSION_mtl(2,2,1)
11
- {-# OPTIONS_GHC -fno-warn-deprecations #-}
12
- #endif
13
-
14
10
module Network.JsonRpc.Types ( RpcResult
15
11
, Method (.. )
16
12
, Methods (.. )
@@ -24,7 +20,6 @@ module Network.JsonRpc.Types ( RpcResult
24
20
, rpcError
25
21
, rpcErrorWithData ) where
26
22
27
- import Data.String (fromString )
28
23
import Data.Maybe (catMaybes )
29
24
import Data.Text (Text , append , unpack )
30
25
import qualified Data.Aeson as A
@@ -33,8 +28,8 @@ import Data.Aeson.Types (emptyObject)
33
28
import qualified Data.Vector as V
34
29
import qualified Data.HashMap.Strict as H
35
30
import Control.DeepSeq (NFData , rnf )
36
- import Control.Monad (mplus , when )
37
- import Control.Monad.Error ( Error , ErrorT , throwError , strMsg , noMsg )
31
+ import Control.Monad (when )
32
+ import Control.Monad.Except ( ExceptT ( .. ) , throwError )
38
33
import Prelude hiding (length )
39
34
import Control.Applicative ((<|>) , empty )
40
35
@@ -44,7 +39,7 @@ import Control.Applicative ((<$>), (<*>), (*>))
44
39
45
40
-- | Return type of a method. A method call can either fail with an 'RpcError'
46
41
-- or succeed with a result of type 'r'.
47
- type RpcResult m r = ErrorT RpcError m r
42
+ type RpcResult m r = ExceptT RpcError m r
48
43
49
44
-- | Parameter expected by a method.
50
45
data Parameter a
@@ -70,34 +65,23 @@ instance (Monad m, Functor m, A.ToJSON r) => MethodParams (RpcResult m r) () m r
70
65
_apply res _ _ = res
71
66
72
67
instance (A. FromJSON a , MethodParams f p m r ) => MethodParams (a -> f ) (a :+: p ) m r where
73
- _apply f (param :+: ps) args = arg >>= \ a -> _apply (f a) ps nextArgs
74
- where arg = either (parseArg name) return =<<
75
- (Left <$> lookupValue) `mplus` (Right <$> paramDefault param)
76
- lookupValue = either (lookupArg name) (headArg name) args
77
- nextArgs = tailOrEmpty <$> args
78
- name = paramName param
79
-
80
- lookupArg :: Monad m => Text -> A. Object -> RpcResult m A. Value
81
- lookupArg name hm = case H. lookup name hm of
82
- Nothing -> throwError $ missingArgError name
83
- Just v -> return v
84
-
85
- headArg :: Monad m => Text -> A. Array -> RpcResult m A. Value
86
- headArg name vec | V. null vec = throwError $ missingArgError name
87
- | otherwise = return $ V. head vec
88
-
89
- tailOrEmpty :: A. Array -> A. Array
90
- tailOrEmpty vec = if V. null vec then V. empty else V. tail vec
91
-
92
- parseArg :: (Monad m , A. FromJSON r ) => Text -> A. Value -> RpcResult m r
68
+ _apply f (param :+: ps) args =
69
+ ExceptT (return arg) >>= \ a -> _apply (f a) ps nextArgs
70
+ where
71
+ arg = maybe (paramDefault param) (parseArg name) lookupValue
72
+ lookupValue = either (H. lookup name) (V. !? 0 ) args
73
+ nextArgs = V. drop 1 <$> args
74
+ name = paramName param
75
+
76
+ parseArg :: A. FromJSON r => Text -> A. Value -> Either RpcError r
93
77
parseArg name val = case A. fromJSON val of
94
78
A. Error msg -> throwError $ argTypeError msg
95
79
A. Success x -> return x
96
80
where argTypeError = rpcErrorWithData (- 32602 ) $ " Wrong type for argument: " `append` name
97
81
98
- paramDefault :: Monad m => Parameter a -> RpcResult m a
99
- paramDefault (Optional _ d) = return d
100
- paramDefault (Required name) = throwError $ missingArgError name
82
+ paramDefault :: Parameter a -> Either RpcError a
83
+ paramDefault (Optional _ d) = Right d
84
+ paramDefault (Required name) = Left $ missingArgError name
101
85
102
86
missingArgError :: Text -> RpcError
103
87
missingArgError name = rpcError (- 32602 ) $ " Cannot find required argument: " `append` name
@@ -175,10 +159,6 @@ data RpcError = RpcError { errCode :: Int
175
159
instance NFData RpcError where
176
160
rnf (RpcError e m d) = rnf e `seq` rnf m `seq` rnf d
177
161
178
- instance Error RpcError where
179
- noMsg = strMsg " unknown error"
180
- strMsg msg = RpcError (- 32000 ) (fromString msg) Nothing
181
-
182
162
instance A. ToJSON RpcError where
183
163
toJSON (RpcError code msg data') = A. object pairs
184
164
where pairs = catMaybes [ Just $ " code" .= code
0 commit comments