Skip to content

Commit

Permalink
Minor token handling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Mar 25, 2023
1 parent 61d65dc commit 1f9d500
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/apiutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ proc genHeaders*(token: Token = nil): HttpHeaders =
})

template updateToken() =
if api != Api.search and resp.headers.hasKey(rlRemaining):
if resp.headers.hasKey(rlRemaining):
let
remaining = parseInt(resp.headers[rlRemaining])
reset = parseInt(resp.headers[rlReset])
Expand Down Expand Up @@ -72,9 +72,9 @@ template fetchImpl(result, fetchBody) {.dirty.} =
if resp.status == "401 Unauthorized" and result.len == 0:
getContent()

if resp.status == $Http503:
badClient = true
raise newException(InternalError, result)
if resp.status == $Http503:
badClient = true
raise newException(BadClientError, "Bad client")

if result.len > 0:
if resp.headers.getOrDefault("content-encoding") == "gzip":
Expand All @@ -90,6 +90,9 @@ template fetchImpl(result, fetchBody) {.dirty.} =
raise newException(InternalError, $url)
except InternalError as e:
raise e
except BadClientError as e:
release(token, used=true)
raise e
except Exception as e:
echo "error: ", e.name, ", msg: ", e.msg, ", token: ", token[], ", url: ", url
if "length" notin e.msg and "descriptor" notin e.msg:
Expand Down
6 changes: 6 additions & 0 deletions src/http_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped =
except ProtocolError:
# Twitter closed the connection, retry
body
except BadClientError:
# Twitter returned 503, we need a new client
pool.release(c, true)
badClient = false
c = pool.acquire(heads)
body
finally:
pool.release(c, badClient)
4 changes: 4 additions & 0 deletions src/nitter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ routes:
resp Http500, showError(
&"An error occurred, please {link} with the URL you tried to visit.", cfg)

error BadClientError:
echo error.exc.name, ": ", error.exc.msg
resp Http500, showError("Network error occured, please try again.", cfg)

error RateLimitError:
const link = a("another instance", href = instancesUrl)
resp Http429, showError(
Expand Down
1 change: 1 addition & 0 deletions src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ genPrefsType()
type
RateLimitError* = object of CatchableError
InternalError* = object of CatchableError
BadClientError* = object of CatchableError

TimelineKind* {.pure.} = enum
tweets
Expand Down

0 comments on commit 1f9d500

Please sign in to comment.