[rest api]: improve exception handling for `/connect` and add `content-type` header for `/download`
This PR achieves the following: 1. Improves the exception handling when dialling a peer fails or an unknown error occurs. 2. Add a `Content-Type` header to the `/download` endpoint of `application/octet-stream`, which is [defined by MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#types) as meant to be used for "generic binary data (or binary data whose true type is unknown)". Co-authored-by: Michael Bradley <michaelsbradleyjr@gmail.com>
This commit is contained in:
parent
2e5c28781c
commit
26ead9726d
|
@ -86,9 +86,14 @@ proc initRestApi*(node: DaggerNodeRef): RestRouter =
|
|||
peerRecord.get().addresses.mapIt(
|
||||
it.address
|
||||
)
|
||||
try:
|
||||
await node.connect(peerId.get(), addresses)
|
||||
return RestApiResponse.response("Successfully connected to peer")
|
||||
except DialFailedError as e:
|
||||
return RestApiResponse.error(Http400, "Unable to dial peer")
|
||||
except CatchableError as e:
|
||||
return RestApiResponse.error(Http400, "Unknown error dialling peer")
|
||||
|
||||
await node.connect(peerId.get(), addresses)
|
||||
return RestApiResponse.response("")
|
||||
|
||||
router.api(
|
||||
MethodGet,
|
||||
|
@ -113,6 +118,7 @@ proc initRestApi*(node: DaggerNodeRef): RestRouter =
|
|||
retr.isErr):
|
||||
return RestApiResponse.error(Http404, retr.error.msg)
|
||||
|
||||
resp.addHeader("Content-Type", "application/octet-stream")
|
||||
await resp.prepareChunked()
|
||||
while not stream.atEof:
|
||||
var
|
||||
|
|
Loading…
Reference in New Issue