From b8bcb1e74bd9e9907196a96a458bc752a6a1e5af Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Fri, 27 Oct 2023 16:31:57 +0200 Subject: [PATCH] Pull new version of nim-presto that implements RestServer' new error handler callback (#2144) Added rest request error handler to capture calls on not installed endpoints better, more descriptive error message returned. --- apps/wakunode2/app.nim | 35 ++++++++++++++++++++++++++++++++++- vendor/nim-presto | 2 +- waku/waku_api/rest/server.nim | 12 ++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/apps/wakunode2/app.nim b/apps/wakunode2/app.nim index 6d8a84799..bb37b9e1c 100644 --- a/apps/wakunode2/app.nim +++ b/apps/wakunode2/app.nim @@ -614,7 +614,33 @@ proc startApp*(app: var App): AppResult[void] = ## Monitoring and external interfaces proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNodeConf): AppResult[RestServerRef] = - let server = ? newRestHttpServer(address, port) + + # Used to register api endpoints that are not currently installed as keys, + # values are holding error messages to be returned to the client + var notInstalledTab: Table[string, string] = initTable[string, string]() + + proc requestErrorHandler(error: RestRequestError, + request: HttpRequestRef): + Future[HttpResponseRef] {.async.} = + case error + of RestRequestError.Invalid: + return await request.respond(Http400, "Invalid request", HttpTable.init()) + of RestRequestError.NotFound: + let rootPath = request.rawPath.split("/")[1] + if notInstalledTab.hasKey(rootPath): + return await request.respond(Http404, notInstalledTab[rootPath], HttpTable.init()) + else: + return await request.respond(Http400, "Bad request initiated. Invalid path or method used.", HttpTable.init()) + of RestRequestError.InvalidContentBody: + return await request.respond(Http400, "Invalid content body", HttpTable.init()) + of RestRequestError.InvalidContentType: + return await request.respond(Http400, "Invalid content type", HttpTable.init()) + of RestRequestError.Unexpected: + return defaultResponse() + + return defaultResponse() + + let server = ? newRestHttpServer(address, port, requestErrorHandler = requestErrorHandler) ## Admin REST API installAdminApiHandlers(server.router, app.node) @@ -641,6 +667,8 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo app.node.subscribe((kind: ContentSub, topic: contentTopic), some(autoHandler)) installRelayApiHandlers(server.router, app.node, cache) + else: + notInstalledTab["relay"] = "/relay endpoints are not available. Please check your configuration: --relay" ## Filter REST API if conf.filternode != "" and @@ -652,6 +680,9 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo let filterCache = rest_filter_api.MessageCache.init() rest_filter_api.installFilterRestApiHandlers(server.router, app.node, filterCache) + else: + notInstalledTab["filter"] = "/filter endpoints are not available. Please check your configuration: --filternode" + ## Store REST API installStoreApiHandlers(server.router, app.node) @@ -660,6 +691,8 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo if conf.lightpushnode != "" and app.node.wakuLightpushClient != nil: rest_lightpush_api.installLightPushRequestHandler(server.router, app.node) + else: + notInstalledTab["lightpush"] = "/lightpush endpoints are not available. Please check your configuration: --lightpushnode" server.start() info "Starting REST HTTP server", url = "http://" & $address & ":" & $port & "/" diff --git a/vendor/nim-presto b/vendor/nim-presto index 2ae448ff5..81250a419 160000 --- a/vendor/nim-presto +++ b/vendor/nim-presto @@ -1 +1 @@ -Subproject commit 2ae448ff5b0808c8f562c6f0a70bbd7a05407a37 +Subproject commit 81250a419bc097a9e93f2ab69de60543eee07138 diff --git a/waku/waku_api/rest/server.nim b/waku/waku_api/rest/server.nim index 430472b68..f25f29ce9 100644 --- a/waku/waku_api/rest/server.nim +++ b/waku/waku_api/rest/server.nim @@ -59,7 +59,8 @@ proc getRouter(allowedOrigin: Option[string]): RestRouter = proc init*(T: type RestServerRef, ip: ValidIpAddress, port: Port, allowedOrigin=none(string), - conf=RestServerConf.default()): RestServerResult[T] = + conf=RestServerConf.default(), + requestErrorHandler: RestRequestErrorHandler = nil): RestServerResult[T] = let address = initTAddress(ip, port) let serverFlags = { HttpServerFlags.QueryCommaSeparatedArray, @@ -82,7 +83,8 @@ proc init*(T: type RestServerRef, serverFlags = serverFlags, httpHeadersTimeout = headersTimeout, maxHeadersSize = maxHeadersSize, - maxRequestBodySize = maxRequestBodySize + maxRequestBodySize = maxRequestBodySize, + requestErrorHandler = requestErrorHandler ) except CatchableError: return err(getCurrentExceptionMsg()) @@ -92,5 +94,7 @@ proc init*(T: type RestServerRef, proc newRestHttpServer*(ip: ValidIpAddress, port: Port, allowedOrigin=none(string), - conf=RestServerConf.default()): RestServerResult[RestServerRef] = - RestServerRef.init(ip, port, allowedOrigin, conf) + conf=RestServerConf.default(), + requestErrorHandler: RestRequestErrorHandler = nil): + RestServerResult[RestServerRef] = + RestServerRef.init(ip, port, allowedOrigin, conf, requestErrorHandler)