Simplify error parser

This commit is contained in:
Zed 2020-06-01 21:53:21 +02:00
parent 2642e5efe4
commit 2fa76db099
4 changed files with 8 additions and 7 deletions

View file

@ -48,7 +48,7 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} =
result = parseJson(body)
if result{"errors"} != nil and result{"errors"}[0]{"code"}.getInt == 200:
if result{"errors"} != nil and result.getError == forbidden:
keepToken = false
echo "bad token"
except:

View file

@ -1,10 +1,6 @@
import json, strutils, options, tables, times, math
import types, parserutils
proc parseError(js: JsonNode): Error =
if js == nil or js.kind != JArray or js.len < 1: return null
return Error(js[0]{"code"}.getInt)
proc parseProfile(js: JsonNode; id=""): Profile =
if js == nil: return
result = Profile(
@ -31,7 +27,7 @@ proc parseUserShow*(js: JsonNode; username: string): Profile =
if js == nil: return
with error, js{"errors"}:
result = Profile(username: username)
if parseError(error) == suspended:
if error.getError == suspended:
result.suspended = true
return
@ -41,7 +37,7 @@ proc parseGraphProfile*(js: JsonNode; username: string): Profile =
if js == nil: return
with error, js{"errors"}:
result = Profile(username: username)
if parseError(error) == suspended:
if error.getError == suspended:
result.suspended = true
return

View file

@ -30,6 +30,10 @@ template `with`*(ident; value: JsonNode; body): untyped =
template getCursor*(js: JsonNode): string =
js{"content", "operation", "cursor", "value"}.getStr
template getError*(js: JsonNode): Error =
if js.kind != JArray or js.len == 0: null
else: Error(js[0]{"code"}.getInt)
template parseTime(time: string; f: static string; flen: int): Time =
if time.len != flen: return
parseTime(time, f, localTimezone).utc.toTime

View file

@ -22,6 +22,7 @@ proc getHmac*(data: string): string =
($hmac(sha256, hmacKey, data))[0 .. 12]
proc getVidUrl*(link: string): string =
if link.len == 0: return
let
sig = getHmac(link)
url = encodeUrl(link)