handle exceptions gracefuly in rest api (#617)

This commit is contained in:
Dmitriy Ryajov 2023-11-14 13:05:43 -06:00 committed by GitHub
parent 8d61391073
commit 4d50ecf504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 126 additions and 91 deletions

View File

@ -160,18 +160,22 @@ proc initSalesApi(node: CodexNodeRef, router: var RestRouter) =
MethodGet, MethodGet,
"/api/codex/v1/sales/slots") do () -> RestApiResponse: "/api/codex/v1/sales/slots") do () -> RestApiResponse:
## Returns active slots for the host ## Returns active slots for the host
try:
without contracts =? node.contracts.host: without contracts =? node.contracts.host:
return RestApiResponse.error(Http503, "Sales unavailable") return RestApiResponse.error(Http503, "Sales unavailable")
let json = %(await contracts.sales.mySlots()) let json = %(await contracts.sales.mySlots())
return RestApiResponse.response($json, contentType="application/json") return RestApiResponse.response($json, contentType="application/json")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
router.api( router.api(
MethodGet, MethodGet,
"/api/codex/v1/sales/availability") do () -> RestApiResponse: "/api/codex/v1/sales/availability") do () -> RestApiResponse:
## Returns storage that is for sale ## Returns storage that is for sale
try:
without contracts =? node.contracts.host: without contracts =? node.contracts.host:
return RestApiResponse.error(Http503, "Sales unavailable") return RestApiResponse.error(Http503, "Sales unavailable")
@ -180,6 +184,9 @@ proc initSalesApi(node: CodexNodeRef, router: var RestRouter) =
let json = %avails let json = %avails
return RestApiResponse.response($json, contentType="application/json") return RestApiResponse.response($json, contentType="application/json")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
router.rawApi( router.rawApi(
MethodPost, MethodPost,
@ -191,6 +198,7 @@ proc initSalesApi(node: CodexNodeRef, router: var RestRouter) =
## minPrice - minimum price to be paid (in amount of tokens) ## minPrice - minimum price to be paid (in amount of tokens)
## maxCollateral - maximum collateral user is willing to pay per filled Slot (in amount of tokens) ## maxCollateral - maximum collateral user is willing to pay per filled Slot (in amount of tokens)
try:
without contracts =? node.contracts.host: without contracts =? node.contracts.host:
return RestApiResponse.error(Http503, "Sales unavailable") return RestApiResponse.error(Http503, "Sales unavailable")
@ -215,6 +223,9 @@ proc initSalesApi(node: CodexNodeRef, router: var RestRouter) =
return RestApiResponse.response(availability.toJson, return RestApiResponse.response(availability.toJson,
contentType="application/json") contentType="application/json")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
proc initPurchasingApi(node: CodexNodeRef, router: var RestRouter) = proc initPurchasingApi(node: CodexNodeRef, router: var RestRouter) =
router.rawApi( router.rawApi(
@ -231,6 +242,7 @@ proc initPurchasingApi(node: CodexNodeRef, router: var RestRouter) =
## tolerance - allowed number of nodes that can be lost before pronouncing the content lost ## tolerance - allowed number of nodes that can be lost before pronouncing the content lost
## colateral - requested collateral from hosts when they fill slot ## colateral - requested collateral from hosts when they fill slot
try:
without cid =? cid.tryGet.catch, error: without cid =? cid.tryGet.catch, error:
return RestApiResponse.error(Http400, error.msg) return RestApiResponse.error(Http400, error.msg)
@ -255,12 +267,16 @@ proc initPurchasingApi(node: CodexNodeRef, router: var RestRouter) =
return RestApiResponse.error(Http500, error.msg) return RestApiResponse.error(Http500, error.msg)
return RestApiResponse.response(purchaseId.toHex) return RestApiResponse.response(purchaseId.toHex)
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
router.api( router.api(
MethodGet, MethodGet,
"/api/codex/v1/storage/purchases/{id}") do ( "/api/codex/v1/storage/purchases/{id}") do (
id: PurchaseId) -> RestApiResponse: id: PurchaseId) -> RestApiResponse:
try:
without contracts =? node.contracts.client: without contracts =? node.contracts.client:
return RestApiResponse.error(Http503, "Purchasing unavailable") return RestApiResponse.error(Http503, "Purchasing unavailable")
@ -278,15 +294,22 @@ proc initPurchasingApi(node: CodexNodeRef, router: var RestRouter) =
) )
return RestApiResponse.response($json, contentType="application/json") return RestApiResponse.response($json, contentType="application/json")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
router.api( router.api(
MethodGet, MethodGet,
"/api/codex/v1/storage/purchases") do () -> RestApiResponse: "/api/codex/v1/storage/purchases") do () -> RestApiResponse:
try:
without contracts =? node.contracts.client: without contracts =? node.contracts.client:
return RestApiResponse.error(Http503, "Purchasing unavailable") return RestApiResponse.error(Http503, "Purchasing unavailable")
let purchaseIds = contracts.purchasing.getPurchaseIds() let purchaseIds = contracts.purchasing.getPurchaseIds()
return RestApiResponse.response($ %purchaseIds, contentType="application/json") return RestApiResponse.response($ %purchaseIds, contentType="application/json")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) = proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
router.api( router.api(
@ -295,6 +318,7 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
## Print rudimentary node information ## Print rudimentary node information
## ##
try:
let table = RestRoutingTable.init(node.discovery.protocol.routingTable) let table = RestRoutingTable.init(node.discovery.protocol.routingTable)
let let
@ -316,6 +340,9 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
} }
return RestApiResponse.response($json, contentType="application/json") return RestApiResponse.response($json, contentType="application/json")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
router.api( router.api(
MethodPost, MethodPost,
@ -328,6 +355,7 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
## `level` - chronicles log level ## `level` - chronicles log level
## ##
try:
without res =? level and level =? res: without res =? level and level =? res:
return RestApiResponse.error(Http400, "Missing log level") return RestApiResponse.error(Http400, "Missing log level")
@ -338,12 +366,16 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
return RestApiResponse.error(Http500, exc.msg) return RestApiResponse.error(Http500, exc.msg)
return RestApiResponse.response("") return RestApiResponse.response("")
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
when codex_enable_api_debug_peers: when codex_enable_api_debug_peers:
router.api( router.api(
MethodGet, MethodGet,
"/api/codex/v1/debug/peer/{peerId}") do (peerId: PeerId) -> RestApiResponse: "/api/codex/v1/debug/peer/{peerId}") do (peerId: PeerId) -> RestApiResponse:
try:
trace "debug/peer start" trace "debug/peer start"
without peerRecord =? (await node.findPeer(peerId.get())): without peerRecord =? (await node.findPeer(peerId.get())):
trace "debug/peer peer not found!" trace "debug/peer peer not found!"
@ -354,6 +386,9 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
let json = %RestPeerRecord.init(peerRecord) let json = %RestPeerRecord.init(peerRecord)
trace "debug/peer returning peer record" trace "debug/peer returning peer record"
return RestApiResponse.response($json) return RestApiResponse.response($json)
except CatchableError as exc:
trace "Excepting processing request", exc = exc.msg
return RestApiResponse.error(Http500)
proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter = proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
var router = RestRouter.init(validate) var router = RestRouter.init(validate)
@ -397,6 +432,6 @@ proc initRestApi*(node: CodexNodeRef, conf: CodexConf): RestRouter =
except DialFailedError: except DialFailedError:
return RestApiResponse.error(Http400, "Unable to dial peer") return RestApiResponse.error(Http400, "Unable to dial peer")
except CatchableError: except CatchableError:
return RestApiResponse.error(Http400, "Unknown error dialling peer") return RestApiResponse.error(Http500, "Unknown error dialling peer")
return router return router