[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:
Eric Mastro 2022-01-20 12:07:30 +11:00 committed by Eric Mastro
parent 2e5c28781c
commit 26ead9726d
1 changed files with 8 additions and 2 deletions

View File

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