Explorar el Código

Simplify error parser

Zed hace 6 años
padre
commit
2fa76db099
Se han modificado 4 ficheros con 8 adiciones y 7 borrados
  1. 1 1
      src/apiutils.nim
  2. 2 6
      src/parser.nim
  3. 4 0
      src/parserutils.nim
  4. 1 0
      src/utils.nim

+ 1 - 1
src/apiutils.nim

@@ -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:

+ 2 - 6
src/parser.nim

@@ -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
 

+ 4 - 0
src/parserutils.nim

@@ -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

+ 1 - 0
src/utils.nim

@@ -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)