From 26ead9726d755cfa1ec39f52a3ed6d0698df3416 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Thu, 20 Jan 2022 12:07:30 +1100 Subject: [PATCH] [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 --- dagger/rest/api.nim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dagger/rest/api.nim b/dagger/rest/api.nim index 7fd2549f..2d18c4dc 100644 --- a/dagger/rest/api.nim +++ b/dagger/rest/api.nim @@ -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