mirror of https://github.com/waku-org/nwaku.git
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:
parent
f7b9afc26f
commit
b8bcb1e74b
|
@ -614,7 +614,33 @@ proc startApp*(app: var App): AppResult[void] =
|
||||||
## Monitoring and external interfaces
|
## Monitoring and external interfaces
|
||||||
|
|
||||||
proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNodeConf): AppResult[RestServerRef] =
|
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
|
## Admin REST API
|
||||||
installAdminApiHandlers(server.router, app.node)
|
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))
|
app.node.subscribe((kind: ContentSub, topic: contentTopic), some(autoHandler))
|
||||||
|
|
||||||
installRelayApiHandlers(server.router, app.node, cache)
|
installRelayApiHandlers(server.router, app.node, cache)
|
||||||
|
else:
|
||||||
|
notInstalledTab["relay"] = "/relay endpoints are not available. Please check your configuration: --relay"
|
||||||
|
|
||||||
## Filter REST API
|
## Filter REST API
|
||||||
if conf.filternode != "" and
|
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()
|
let filterCache = rest_filter_api.MessageCache.init()
|
||||||
rest_filter_api.installFilterRestApiHandlers(server.router, app.node, filterCache)
|
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
|
## Store REST API
|
||||||
installStoreApiHandlers(server.router, app.node)
|
installStoreApiHandlers(server.router, app.node)
|
||||||
|
@ -660,6 +691,8 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo
|
||||||
if conf.lightpushnode != "" and
|
if conf.lightpushnode != "" and
|
||||||
app.node.wakuLightpushClient != nil:
|
app.node.wakuLightpushClient != nil:
|
||||||
rest_lightpush_api.installLightPushRequestHandler(server.router, app.node)
|
rest_lightpush_api.installLightPushRequestHandler(server.router, app.node)
|
||||||
|
else:
|
||||||
|
notInstalledTab["lightpush"] = "/lightpush endpoints are not available. Please check your configuration: --lightpushnode"
|
||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
info "Starting REST HTTP server", url = "http://" & $address & ":" & $port & "/"
|
info "Starting REST HTTP server", url = "http://" & $address & ":" & $port & "/"
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 2ae448ff5b0808c8f562c6f0a70bbd7a05407a37
|
Subproject commit 81250a419bc097a9e93f2ab69de60543eee07138
|
|
@ -59,7 +59,8 @@ proc getRouter(allowedOrigin: Option[string]): RestRouter =
|
||||||
proc init*(T: type RestServerRef,
|
proc init*(T: type RestServerRef,
|
||||||
ip: ValidIpAddress, port: Port,
|
ip: ValidIpAddress, port: Port,
|
||||||
allowedOrigin=none(string),
|
allowedOrigin=none(string),
|
||||||
conf=RestServerConf.default()): RestServerResult[T] =
|
conf=RestServerConf.default(),
|
||||||
|
requestErrorHandler: RestRequestErrorHandler = nil): RestServerResult[T] =
|
||||||
let address = initTAddress(ip, port)
|
let address = initTAddress(ip, port)
|
||||||
let serverFlags = {
|
let serverFlags = {
|
||||||
HttpServerFlags.QueryCommaSeparatedArray,
|
HttpServerFlags.QueryCommaSeparatedArray,
|
||||||
|
@ -82,7 +83,8 @@ proc init*(T: type RestServerRef,
|
||||||
serverFlags = serverFlags,
|
serverFlags = serverFlags,
|
||||||
httpHeadersTimeout = headersTimeout,
|
httpHeadersTimeout = headersTimeout,
|
||||||
maxHeadersSize = maxHeadersSize,
|
maxHeadersSize = maxHeadersSize,
|
||||||
maxRequestBodySize = maxRequestBodySize
|
maxRequestBodySize = maxRequestBodySize,
|
||||||
|
requestErrorHandler = requestErrorHandler
|
||||||
)
|
)
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
return err(getCurrentExceptionMsg())
|
return err(getCurrentExceptionMsg())
|
||||||
|
@ -92,5 +94,7 @@ proc init*(T: type RestServerRef,
|
||||||
|
|
||||||
proc newRestHttpServer*(ip: ValidIpAddress, port: Port,
|
proc newRestHttpServer*(ip: ValidIpAddress, port: Port,
|
||||||
allowedOrigin=none(string),
|
allowedOrigin=none(string),
|
||||||
conf=RestServerConf.default()): RestServerResult[RestServerRef] =
|
conf=RestServerConf.default(),
|
||||||
RestServerRef.init(ip, port, allowedOrigin, conf)
|
requestErrorHandler: RestRequestErrorHandler = nil):
|
||||||
|
RestServerResult[RestServerRef] =
|
||||||
|
RestServerRef.init(ip, port, allowedOrigin, conf, requestErrorHandler)
|
||||||
|
|
Loading…
Reference in New Issue