Provide a specific error in JSON-RPC API when content is not found (#2059)

This commit is contained in:
Kim De Mey 2024-03-06 08:57:09 +01:00 committed by GitHub
parent 0debf1a122
commit 88a93beb26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -15,6 +15,9 @@ import
../network/wire/portal_protocol, ../network/wire/portal_protocol,
./rpc_types ./rpc_types
{.warning[UnusedImport]: off.}
import json_rpc/errors
export rpcserver, tables export rpcserver, tables
# Portal Network JSON-RPC impelentation as per specification: # Portal Network JSON-RPC impelentation as per specification:
@ -179,10 +182,10 @@ proc installPortalApiHandlers*(
let let
key = ByteList.init(hexToSeqByte(contentKey)) key = ByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr: contentId = p.toContentId(key).valueOr:
raise newException(ValueError, "Invalid content key") raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
contentResult = (await p.contentLookup(key, contentId)).valueOr: contentResult = (await p.contentLookup(key, contentId)).valueOr:
return ContentInfo(content: "0x", utpTransfer: false) raise (ref ApplicationError)(code: -39001, msg: "Content not found")
return ContentInfo( return ContentInfo(
content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer content: contentResult.content.to0xHex(), utpTransfer: contentResult.utpTransfer
@ -194,9 +197,17 @@ proc installPortalApiHandlers*(
let let
key = ByteList.init(hexToSeqByte(contentKey)) key = ByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr: contentId = p.toContentId(key).valueOr:
raise newException(ValueError, "Invalid content key") raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
await p.traceContentLookup(key, contentId) res = await p.traceContentLookup(key, contentId)
# TODO: Might want to restructure the lookup result here. Potentially doing
# the json conversion in this module.
if res.content.isSome():
return res
else:
let data = Opt.some(JrpcConv.encode(res.trace).JsonString)
raise (ref ApplicationError)(code: -39001, msg: "Content not found", data: data)
rpcServer.rpc("portal_" & network & "Store") do( rpcServer.rpc("portal_" & network & "Store") do(
contentKey: string, contentValue: string contentKey: string, contentValue: string
@ -208,16 +219,16 @@ proc installPortalApiHandlers*(
p.storeContent(key, contentId.get(), hexToSeqByte(contentValue)) p.storeContent(key, contentId.get(), hexToSeqByte(contentValue))
return true return true
else: else:
raise newException(ValueError, "Invalid content key") raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
rpcServer.rpc("portal_" & network & "LocalContent") do(contentKey: string) -> string: rpcServer.rpc("portal_" & network & "LocalContent") do(contentKey: string) -> string:
let let
key = ByteList.init(hexToSeqByte(contentKey)) key = ByteList.init(hexToSeqByte(contentKey))
contentId = p.toContentId(key).valueOr: contentId = p.toContentId(key).valueOr:
raise newException(ValueError, "Invalid content key") raise (ref errors.InvalidRequest)(code: -32602, msg: "Invalid content key")
contentResult = p.dbGet(key, contentId).valueOr: contentResult = p.dbGet(key, contentId).valueOr:
return "0x" raise (ref ApplicationError)(code: -39001, msg: "Content not found")
return contentResult.to0xHex() return contentResult.to0xHex()

2
vendor/nim-json-rpc vendored

@ -1 +1 @@
Subproject commit 8682bb6c858fbcff2f0070c241ce6aae030753a3 Subproject commit a20ba207d25a9ab496518e84ec0c28b2d8d25ad7