Simplify new error handling
This commit is contained in:
parent
3d91ae0256
commit
fff04de24b
|
@ -1,8 +1,8 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import httpclient, asyncdispatch, options, sequtils, strutils, uri
|
import httpclient, asyncdispatch, options, strutils, uri
|
||||||
import jsony, packedjson, zippy
|
import jsony, packedjson, zippy
|
||||||
import types, tokens, consts, parserutils, http_pool
|
import types, tokens, consts, parserutils, http_pool
|
||||||
from experimental/types/common import Errors, ErrorObj
|
import experimental/types/common
|
||||||
|
|
||||||
const
|
const
|
||||||
rlRemaining = "x-rate-limit-remaining"
|
rlRemaining = "x-rate-limit-remaining"
|
||||||
|
@ -108,8 +108,8 @@ proc fetchRaw*(url: Uri; api: Api): Future[string] {.async.} =
|
||||||
updateToken()
|
updateToken()
|
||||||
|
|
||||||
if result.startsWith("{\"errors"):
|
if result.startsWith("{\"errors"):
|
||||||
let errors = result.fromJson(Errors).errors
|
let errors = result.fromJson(Errors)
|
||||||
if errors.anyIt(it.code in {invalidToken, forbidden, badToken}):
|
if errors in {invalidToken, forbidden, badToken}:
|
||||||
echo "fetch error: ", errors
|
echo "fetch error: ", errors
|
||||||
release(token, invalid=true)
|
release(token, invalid=true)
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
|
|
|
@ -40,7 +40,7 @@ proc getBanner(user: User): string =
|
||||||
|
|
||||||
proc parseUser*(json: string): Profile =
|
proc parseUser*(json: string): Profile =
|
||||||
handleErrors:
|
handleErrors:
|
||||||
case error
|
case error.code
|
||||||
of suspended: return Profile(suspended: true)
|
of suspended: return Profile(suspended: true)
|
||||||
of userNotFound: return
|
of userNotFound: return
|
||||||
else: echo "[error - parseUser]: ", error
|
else: echo "[error - parseUser]: ", error
|
||||||
|
|
|
@ -18,5 +18,5 @@ proc getImageUrl*(url: string): string =
|
||||||
|
|
||||||
template handleErrors*(body) =
|
template handleErrors*(body) =
|
||||||
if json.startsWith("{\"errors"):
|
if json.startsWith("{\"errors"):
|
||||||
let error {.inject.} = json.fromJson(Errors).errors[0].code
|
for error {.inject.} in json.fromJson(Errors).errors:
|
||||||
body
|
body
|
||||||
|
|
|
@ -13,3 +13,8 @@ type
|
||||||
|
|
||||||
Errors* = object
|
Errors* = object
|
||||||
errors*: seq[ErrorObj]
|
errors*: seq[ErrorObj]
|
||||||
|
|
||||||
|
proc contains*(codes: set[Error]; errors: Errors): bool =
|
||||||
|
for e in errors.errors:
|
||||||
|
if e.code in codes:
|
||||||
|
return true
|
||||||
|
|
Loading…
Reference in New Issue