Error stuff

This commit is contained in:
Zed 2020-06-01 09:45:38 +02:00
parent 0859d8c2d9
commit 39863703b3
2 changed files with 12 additions and 7 deletions

View file

@ -1,6 +1,10 @@
import json, strutils, options, tables, times, math import json, strutils, options, tables, times, math
import types, parserutils 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 = proc parseProfile(js: JsonNode; id=""): Profile =
if js == nil: return if js == nil: return
result = Profile( result = Profile(
@ -24,12 +28,12 @@ proc parseProfile(js: JsonNode; id=""): Profile =
result.expandProfileEntities(js) result.expandProfileEntities(js)
proc parseGraphProfile*(js: JsonNode; username: string): Profile = proc parseGraphProfile*(js: JsonNode; username: string): Profile =
with errors, js{"errors"}: if js == nil: return
for error in errors: with error, js{"errors"}:
case Error(error{"code"}.getInt) result = Profile(username: username)
of notFound: return Profile(username: username) if parseError(error) == suspended:
of suspended: return Profile(username: username, suspended: true) result.suspended = true
else: discard return
let user = js{"data", "user", "legacy"} let user = js{"data", "user", "legacy"}
let id = js{"data", "user", "rest_id"}.getStr let id = js{"data", "user", "rest_id"}.getStr

View file

@ -13,10 +13,11 @@ type
# agent*: string # agent*: string
Error* = enum Error* = enum
null = 0
protectedUser = 22 protectedUser = 22
couldntAuth = 32 couldntAuth = 32
doesntExist = 34 doesntExist = 34
notFound = 50 userNotFound = 50
suspended = 63 suspended = 63
invalidToken = 89 invalidToken = 89
listIdOrSlug = 112 listIdOrSlug = 112