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.
This commit is contained in:
NagyZoltanPeter 2023-10-27 16:31:57 +02:00 committed by GitHub
parent f7b9afc26f
commit b8bcb1e74b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 6 deletions

View File

@ -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 & "/"

2
vendor/nim-presto vendored

@ -1 +1 @@
Subproject commit 2ae448ff5b0808c8f562c6f0a70bbd7a05407a37
Subproject commit 81250a419bc097a9e93f2ab69de60543eee07138

View File

@ -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)