Skip to content

Commit

Permalink
fix: wrap untracked protocol handler exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Delgado authored Apr 11, 2023
1 parent 418efca commit 9e1432c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 13 additions & 5 deletions waku/v2/protocol/waku_lightpush/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const WakuLightPushCodec* = "/vac/waku/lightpush/2.0.0-beta1"

type
WakuLightPushResult*[T] = Result[T, string]

PushMessageHandler* = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.gcsafe, closure.}

WakuLightPush* = ref object of LPProtocol
Expand Down Expand Up @@ -57,7 +57,15 @@ proc initProtocolHandler*(wl: WakuLightPush) =
debug "push request", peerId=conn.peerId, requestId=req.requestId, pubsubTopic=pubsubTopic

var response: PushResponse
let handleRes = await wl.pushHandler(conn.peerId, pubsubTopic, message)
var handleRes: WakuLightPushResult[void]
try:
handleRes = await wl.pushHandler(conn.peerId, pubsubTopic, message)
except Exception:
response = PushResponse(is_success: false, info: some(getCurrentExceptionMsg()))
waku_lightpush_errors.inc(labelValues = [messagePushFailure])
error "pushed message handling failed", error= getCurrentExceptionMsg()


if handleRes.isOk():
response = PushResponse(is_success: true, info: some("OK"))
else:
Expand All @@ -71,10 +79,10 @@ proc initProtocolHandler*(wl: WakuLightPush) =
wl.handler = handle
wl.codec = WakuLightPushCodec

proc new*(T: type WakuLightPush,
peerManager: PeerManager,
proc new*(T: type WakuLightPush,
peerManager: PeerManager,
rng: ref rand.HmacDrbgContext,
pushHandler: PushMessageHandler): T =
pushHandler: PushMessageHandler): T =
let wl = WakuLightPush(rng: rng, peerManager: peerManager, pushHandler: pushHandler)
wl.initProtocolHandler()
return wl
12 changes: 11 additions & 1 deletion waku/v2/protocol/waku_store/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ proc initProtocolHandler(ws: WakuStore) =
info "received history query", peerId=conn.peerId, requestId=requestId, query=request
waku_store_queries.inc()

let responseRes = ws.queryHandler(request)
var responseRes: HistoryResult
try:
responseRes = ws.queryHandler(request)
except Exception:
error "history query failed", peerId= $conn.peerId, requestId=requestId, error=getCurrentExceptionMsg()

let error = HistoryError(kind: HistoryErrorKind.UNKNOWN).toRPC()
let response = HistoryResponseRPC(error: error)
let rpc = HistoryRPC(requestId: requestId, response: some(response))
await conn.writeLp(rpc.encode().buffer)
return

if responseRes.isErr():
error "history query failed", peerId= $conn.peerId, requestId=requestId, error=responseRes.error
Expand Down

0 comments on commit 9e1432c

Please sign in to comment.