Skip to content

Commit 1ade31a

Browse files
committed
Cleanup
1 parent 50bd3a1 commit 1ade31a

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/Network/JsonRpc/Server.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ callWithBatchStrategy strategy fs input = either returnErr callMethod request
102102
parseJson = maybe invalidJson return . A.decode
103103
parseVal val = case val of
104104
obj@(A.Object _) -> return $ Left obj
105-
A.Array vec | V.null vec -> throwError $ invalidRpcError "Empty batch request"
106-
| otherwise -> return $ Right $ V.toList vec
107-
_ -> throwError $ invalidRpcError "Not a JSON object or array"
105+
A.Array vec | V.null vec -> throwInvalidRpc "Empty batch request"
106+
| otherwise -> return $ Right $ V.toList vec
107+
_ -> throwInvalidRpc "Not a JSON object or array"
108108
callMethod rq = case rq of
109109
Left val -> encodeJust `liftM` singleCall fs val
110110
Right vals -> encodeJust `liftM` batchCall strategy fs vals
111-
where encodeJust r = (A.encode . A.toJSON) <$> r
112-
returnErr = return . Just . A.encode . A.toJSON . nullIdResponse
111+
where encodeJust r = A.encode <$> r
112+
returnErr = return . Just . A.encode . nullIdResponse
113113
invalidJson = throwError $ rpcError (-32700) "Invalid JSON"
114114

115115
singleCall :: Monad m => Methods m -> A.Value -> m (Maybe Response)
@@ -126,15 +126,15 @@ nullIdResponse err = toResponse (Just IdNull) (Left err :: Either RpcError ())
126126

127127
parseValue :: (A.FromJSON a, Monad m) => A.Value -> RpcResult m a
128128
parseValue val = case A.fromJSON val of
129-
A.Error msg -> throwError $ invalidRpcError $ pack msg
129+
A.Error msg -> throwInvalidRpc $ pack msg
130130
A.Success x -> return x
131131

132132
lookupMethod :: Monad m => Text -> H.HashMap Text (Method m) -> RpcResult m (Method m)
133133
lookupMethod name = maybe notFound return . H.lookup name
134-
where notFound = throwError $ rpcError (-32601) ("Method not found: " `append` name)
134+
where notFound = throwError $ rpcError (-32601) $ "Method not found: " `append` name
135135

136-
invalidRpcError :: Text -> RpcError
137-
invalidRpcError = rpcErrorWithData (-32600) "Invalid JSON RPC 2.0 request"
136+
throwInvalidRpc :: Monad m => Text -> RpcResult m a
137+
throwInvalidRpc = throwError . rpcErrorWithData (-32600) "Invalid JSON RPC 2.0 request"
138138

139139
batchCall :: Monad m => (forall a. [m a] -> m [a])
140140
-> Methods m

src/Network/JsonRpc/Types.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
FlexibleInstances,
44
UndecidableInstances,
55
TypeOperators,
6-
PatternGuards,
76
OverloadedStrings #-}
87

98
module Network.JsonRpc.Types ( RpcResult
@@ -55,9 +54,9 @@ class (Monad m, Functor m, A.ToJSON r) => MethodParams f p m r | f -> p m r wher
5554
apply :: f -> p -> Args -> RpcResult m r
5655

5756
instance (Monad m, Functor m, A.ToJSON r) => MethodParams (RpcResult m r) () m r where
58-
apply r _ args | Left _ <- args = r
59-
| Right ar <- args, V.null ar = r
60-
| otherwise = throwError $ rpcError (-32602) "Too many unnamed arguments"
57+
apply _ _ (Right ar) | not $ V.null ar =
58+
throwError $ rpcError (-32602) "Too many unnamed arguments"
59+
apply res _ _ = res
6160

6261
instance (A.FromJSON a, MethodParams f p m r) => MethodParams (a -> f) (a :+: p) m r where
6362
apply f (param :+: ps) args = arg >>= \a -> apply (f a) ps nextArgs
@@ -81,15 +80,16 @@ tailOrEmpty vec = if V.null vec then V.empty else V.tail vec
8180

8281
parseArg :: (Monad m, A.FromJSON r) => Text -> A.Value -> RpcResult m r
8382
parseArg name val = case A.fromJSON val of
84-
A.Error msg -> throwError $ rpcErrorWithData (-32602) ("Wrong type for argument: " `append` name) msg
83+
A.Error msg -> throwError $ argTypeError msg
8584
A.Success x -> return x
85+
where argTypeError = rpcErrorWithData (-32602) $ "Wrong type for argument: " `append` name
8686

8787
paramDefault :: Monad m => Parameter a -> RpcResult m a
8888
paramDefault (Optional _ d) = return d
8989
paramDefault (Required name) = throwError $ missingArgError name
9090

9191
missingArgError :: Text -> RpcError
92-
missingArgError name = rpcError (-32602) ("Cannot find required argument: " `append` name)
92+
missingArgError name = rpcError (-32602) $ "Cannot find required argument: " `append` name
9393

9494
paramName :: Parameter a -> Text
9595
paramName (Optional n _) = n
@@ -114,7 +114,8 @@ instance A.FromJSON Request where
114114
where parseParams (A.Object obj) = return $ Left obj
115115
parseParams (A.Array ar) = return $ Right ar
116116
parseParams _ = empty
117-
checkVersion ver = when (ver /= jsonRpcVersion) (fail $ "Wrong JSON RPC version: " ++ unpack ver)
117+
checkVersion ver = when (ver /= jsonRpcVersion) $
118+
fail $ "Wrong JSON RPC version: " ++ unpack ver
118119
-- (.:?) parses Null value as Nothing so parseId needs
119120
-- to use both (.:?) and (.:) to handle all cases
120121
parseId = x .:? idKey >>= \optional ->

0 commit comments

Comments
 (0)