From 99d3c46af5fefbd99a99bdbd8831e56c1616460a Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 28 Dec 2021 05:41:41 +0100 Subject: [PATCH] Improve API error handling --- src/apiutils.nim | 14 +++++++++----- src/nitter.nim | 10 ++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/apiutils.nim b/src/apiutils.nim index c375000..d253c21 100644 --- a/src/apiutils.nim +++ b/src/apiutils.nim @@ -46,7 +46,9 @@ proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} = var resp: AsyncResponse let body = pool.use(headers): resp = await c.get($url) - uncompress(await resp.body) + let raw = await resp.body + if raw.len == 0: "" + else: uncompress(raw) if body.startsWith('{') or body.startsWith('['): result = parseJson(body) @@ -64,11 +66,13 @@ proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} = echo "fetch error: ", result.getError release(token, true) raise rateLimitError() - except ZippyError as e: - echo "decompression error: ", e.msg, ", url: ", url - raise newException(InternalError, "decompression failed: " & $url) + + if resp.status == $Http400: + raise newException(InternalError, $url) + except InternalError as e: + raise e except Exception as e: - echo "error: ", e.msg, ", token: ", token[], ", url: ", url + echo "error: ", e.name, ", msg: ", e.msg, ", token: ", token[], ", url: ", url if "length" notin e.msg and "descriptor" notin e.msg: release(token, true) raise rateLimitError() diff --git a/src/nitter.nim b/src/nitter.nim index 08d7c2b..2e69576 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -77,14 +77,16 @@ routes: resp Http404, showError("Page not found", cfg) error InternalError: - echo error.exc.msg + echo error.exc.name, ": ", error.exc.msg const link = a("open a GitHub issue", href = issuesUrl) - resp Http500, showError(&"An error occurred, please {link} with the URL you tried to visit.", cfg) + resp Http500, showError( + &"An error occurred, please {link} with the URL you tried to visit.", cfg) error RateLimitError: - echo error.exc.msg + echo error.exc.name, ": ", error.exc.msg const link = a("another instance", href = instancesUrl) - resp Http429, showError(&"Instance has been rate limited.
Use {link} or try again later.", cfg) + resp Http429, showError( + &"Instance has been rate limited.
Use {link} or try again later.", cfg) extend unsupported, "" extend preferences, ""