Skip to content

Commit 6644620

Browse files
committed
Exposed RpcError constructor for client library
1 parent 42e0ad6 commit 6644620

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

json-rpc-server.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- documentation, see http://haskell.org/cabal/users-guide/
33

44
name: json-rpc-server
5-
version: 0.1.3.0
5+
version: 0.1.4.0
66
license: MIT
77
license-file: LICENSE
88
category: Network, JSON
@@ -16,7 +16,7 @@ description: An implementation of the server side of JSON-RPC 2.0.
1616
See <http://www.jsonrpc.org/specification>. This
1717
library uses 'ByteString' for input and output,
1818
leaving the choice of transport up to the user.
19-
See the 'Network.JsonRpc.Server' module for an example.
19+
See the "Network.JsonRpc.Server" module for an example.
2020

2121
source-repository head
2222
type: git

src/Network/JsonRpc/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Network.JsonRpc.Server (
3232
, (:+:) (..)
3333
, MethodParams
3434
-- ** Errors
35-
, RpcError
35+
, RpcError (..)
3636
, rpcError
3737
, rpcErrorWithData) where
3838

src/Network/JsonRpc/Types.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Network.JsonRpc.Types ( RpcResult
2020
, Request (..)
2121
, Response (..)
2222
, Id (..)
23-
, RpcError
23+
, RpcError (..)
2424
, rpcError
2525
, rpcErrorWithData) where
2626

@@ -56,7 +56,7 @@ infixr :+:
5656
-- monad ('m'), and return type ('r'). 'p' has one 'Parameter' for
5757
-- every argument of 'f' and is terminated by @()@. The return type
5858
-- of 'f' is @RpcResult m r@. This class is treated as closed.
59-
class (Monad m, Functor m, A.ToJSON r) => MethodParams f p m r | f -> p m r where
59+
class (Monad m, Functor m, A.ToJSON r) => MethodParams f p m r | f -> p m r, p m r -> f where
6060
apply :: f -> p -> Args -> RpcResult m r
6161

6262
instance (Monad m, Functor m, A.ToJSON r) => MethodParams (RpcResult m r) () m r where
@@ -153,9 +153,11 @@ instance A.ToJSON Id where
153153
toJSON (IdNumber x) = x
154154
toJSON IdNull = A.Null
155155

156-
-- | Error to be returned to the client.
157-
data RpcError = RpcError Int Text (Maybe A.Value)
158-
deriving Show
156+
-- | JSON-RPC error.
157+
data RpcError = RpcError { errCode :: Int
158+
, errMsg :: Text
159+
, errData :: Maybe A.Value }
160+
deriving (Show, Eq)
159161

160162
instance Error RpcError where
161163
noMsg = strMsg "unknown error"
@@ -167,6 +169,13 @@ instance A.ToJSON RpcError where
167169
, Just $ "message" .= msg
168170
, ("data" .=) <$> data' ]
169171

172+
instance A.FromJSON RpcError where
173+
parseJSON = A.withObject "JSON-RPC error object" $
174+
\v -> RpcError <$>
175+
v .: "code" <*>
176+
v .: "message" <*>
177+
v .:? "data"
178+
170179
-- | Creates an 'RpcError' with the given error code and message.
171180
-- According to the specification, server error codes should be
172181
-- in the range -32099 to -32000, and application defined errors

0 commit comments

Comments
 (0)