Skip to content

Commit

Permalink
fix(rest): filter/v2/subscriptions response (#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Mar 18, 2024
1 parent a576e62 commit 7aea2d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
4 changes: 2 additions & 2 deletions tests/wakunode_rest/test_rest_filter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ suite "Waku v2 Rest API - Filter V2":
pingResponse.status == 200
$pingResponse.contentType == $MIMETYPE_JSON
pingResponse.data.requestId == "9999"
pingResponse.data.statusDesc.len() == 0
pingResponse.data.statusDesc == "OK"

# When - error case
let requestBodyUnsubAll = FilterUnsubscribeAllRequest(requestId: "9988")
Expand Down Expand Up @@ -288,7 +288,7 @@ suite "Waku v2 Rest API - Filter V2":
pingResponse.status == 200
$pingResponse.contentType == $MIMETYPE_JSON
pingResponse.data.requestId == "9999"
pingResponse.data.statusDesc.len() == 0
pingResponse.data.statusDesc == "OK"

# When - message push
let testMessage = WakuMessage(
Expand Down
21 changes: 8 additions & 13 deletions waku/waku_api/rest/filter/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ func decodeRequestBody[T](

return ok(requestResult.get())

proc getErrorCause(err: filter_protocol_type.FilterSubscribeError): string =
proc getStatusDesc(protocolClientRes: filter_protocol_type.FilterSubscribeResult): string =
## Retrieve proper error cause of FilterSubscribeError - due stringify make some parts of text double
if protocolClientRes.isOk:
return "OK"

let err = protocolClientRes.error
case err.kind
of FilterSubscribeErrorKind.PEER_DIAL_FAILURE:
err.address
Expand All @@ -81,27 +84,19 @@ proc convertResponse(
protocolClientRes: filter_protocol_type.FilterSubscribeResult,
): T =
## Properly convert filter protocol's response to rest response

if protocolClientRes.isErr():
return FilterSubscriptionResponse(
requestId: requestId,
statusCode: uint32(protocolClientRes.error().kind),
statusDesc: getErrorCause(protocolClientRes.error()),
)
else:
return
FilterSubscriptionResponse(requestId: requestId, statusCode: 0, statusDesc: "")
return FilterSubscriptionResponse(
requestId: requestId,
statusDesc: getStatusDesc(protocolClientRes),
)

proc convertResponse(
T: type FilterSubscriptionResponse,
requestId: string,
protocolClientRes: filter_protocol_type.FilterSubscribeError,
): T =
## Properly convert filter protocol's response to rest response in case of error

return FilterSubscriptionResponse(
requestId: requestId,
statusCode: uint32(protocolClientRes.kind),
statusDesc: $protocolClientRes,
)

Expand Down
6 changes: 0 additions & 6 deletions waku/waku_api/rest/filter/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type FilterUnsubscribeAllRequest* = object

type FilterSubscriptionResponse* = object
requestId*: string
statusCode*: uint32
statusDesc*: string

#### Type conversion
Expand Down Expand Up @@ -119,7 +118,6 @@ proc writeValue*(
) {.raises: [IOError].} =
writer.beginRecord()
writer.writeField("requestId", value.requestId)
writer.writeField("statusCode", value.statusCode)
writer.writeField("statusDesc", value.statusDesc)
writer.endRecord()

Expand Down Expand Up @@ -406,7 +404,6 @@ proc readValue*(
) {.raises: [SerializationError, IOError].} =
var
requestId = none(string)
statusCode = none(uint32)
statusDesc = none(string)

var keys = initHashSet[string]()
Expand All @@ -423,8 +420,6 @@ proc readValue*(
case fieldName
of "requestId":
requestId = some(reader.readValue(string))
of "statusCode":
statusCode = some(reader.readValue(uint32))
of "statusDesc":
statusDesc = some(reader.readValue(string))
else:
Expand All @@ -435,6 +430,5 @@ proc readValue*(

value = FilterSubscriptionResponse(
requestId: requestId.get(),
statusCode: statusCode.get(),
statusDesc: statusDesc.get(""),
)

0 comments on commit 7aea2d4

Please sign in to comment.