55# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
66# at your option. This file may not be copied, modified, or distributed except according to those terms.
77
8+ {.push raises : [].}
9+
810import
911 json_serialization,
1012 chronos,
@@ -14,41 +16,32 @@ import
1416 json_rpc/ rpcclient,
1517 ../ common/ common_types,
1618 ../ network/ history/ [history_content, history_validation],
17- ./ rpc_calls/ [rpc_discovery_calls, rpc_portal_calls, rpc_portal_debug_calls]
19+ ./ rpc_calls/ [rpc_discovery_calls, rpc_portal_calls, rpc_portal_debug_calls],
20+ ./ rpc_types
1821
19- export rpcclient, rpc_discovery_calls, rpc_portal_calls, rpc_portal_debug_calls, results
22+ export
23+ rpcclient, rpc_discovery_calls, rpc_portal_calls, rpc_portal_debug_calls, results,
24+ rpc_types
2025
2126type
2227 PortalRpcClient * = distinct RpcClient
2328
24- PortalRpcError * = enum
25- ContentNotFound
26- InvalidContentKey
27- InvalidContentValue
28- ContentValidationFailed
29-
30- ErrorResponse = object
29+ PortalErrorResponse * = object
3130 code* : int
3231 message* : string
3332
33+ const
34+ InvalidJsonRpcError * = - 1
35+ ReceivedInvalidDataError * = - 2
36+
3437proc init * (T: type PortalRpcClient , rpcClient: RpcClient ): T =
3538 T (rpcClient)
3639
37- func toPortalRpcError (e: ref CatchableError ): PortalRpcError =
38- let error =
39- try :
40- Json .decode (e.msg, ErrorResponse )
41- except SerializationError as e:
42- raiseAssert (e.msg)
43-
44- if error.code == - 39001 :
45- ContentNotFound
46- elif error.code == - 32602 :
47- InvalidContentKey
48- elif error.code == - 32603 :
49- ContentValidationFailed
50- else :
51- raiseAssert (e.msg)
40+ func toPortalRpcError * (error: string ): PortalErrorResponse =
41+ try :
42+ Json .decode (error, PortalErrorResponse )
43+ except SerializationError as e:
44+ PortalErrorResponse (code: InvalidJsonRpcError , message: error)
5245
5346template toBytes (content: string ): seq [byte ] =
5447 try :
@@ -58,7 +51,7 @@ template toBytes(content: string): seq[byte] =
5851
5952proc historyGetBlockBody * (
6053 client: PortalRpcClient , header: Header
61- ): Future [Result [BlockBody , PortalRpcError ]] {.async : (raises: []).} =
54+ ): Future [Result [BlockBody , PortalErrorResponse ]] {.async : (raises: []).} =
6255 # # Fetches the block body for the given block header from the Portal History
6356 # # Network. The data is first looked up in the node's local database before
6457 # # trying to fetch it from the network. The block header needs to be passed
@@ -69,15 +62,20 @@ proc historyGetBlockBody*(
6962 try :
7063 await RpcClient (client).portal_historyGetBlockBody (headerBytes)
7164 except CatchableError as e:
72- return err (e.toPortalRpcError ())
65+ return err (e.msg. toPortalRpcError ())
7366 blockBody = decodeRlp (content.toBytes (), BlockBody ).valueOr:
74- return err (InvalidContentValue )
67+ return err (
68+ PortalErrorResponse (
69+ code: ReceivedInvalidDataError ,
70+ message: " Failed to decode received BlockBody: " & error,
71+ )
72+ )
7573
7674 ok (blockBody)
7775
7876proc historyGetReceipts * (
7977 client: PortalRpcClient , header: Header
80- ): Future [Result [StoredReceipts , PortalRpcError ]] {.async : (raises: []).} =
78+ ): Future [Result [StoredReceipts , PortalErrorResponse ]] {.async : (raises: []).} =
8179 # # Fetches the receipts for the given block header from the Portal History
8280 # # Network. The data is first looked up in the node's local database before
8381 # # trying to fetch it from the network. The block header needs to be passed
@@ -88,8 +86,13 @@ proc historyGetReceipts*(
8886 try :
8987 await RpcClient (client).portal_historyGetReceipts (headerBytes)
9088 except CatchableError as e:
91- return err (e.toPortalRpcError ())
89+ return err (e.msg. toPortalRpcError ())
9290 receipts = decodeRlp (content.toBytes (), StoredReceipts ).valueOr:
93- return err (InvalidContentValue )
91+ return err (
92+ PortalErrorResponse (
93+ code: ReceivedInvalidDataError ,
94+ message: " Failed to decode received StoredReceipts: " & error,
95+ )
96+ )
9497
9598 ok (receipts)
0 commit comments