From 06a54a5c325cdcf760f05d6d4f5c9a0176524440 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 25 Jun 2019 00:55:41 +0200 Subject: [PATCH] Fix crash when profile doesn't exist --- public/style.css | 2 +- src/api.nim | 6 ++++++ src/cache.nim | 20 +++++++++----------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/public/style.css b/public/style.css index fba4cc6..2d70e3c 100644 --- a/public/style.css +++ b/public/style.css @@ -539,7 +539,7 @@ nav { box-sizing: border-box; display: flex; margin: 0; - padding: 0; + padding: 0 2px; width: 100%; } diff --git a/src/api.nim b/src/api.nim index 68671c3..c92095b 100644 --- a/src/api.nim +++ b/src/api.nim @@ -118,6 +118,9 @@ proc getProfileFallback(username: string; headers: HttpHeaders): Future[Profile] url = base / profileIntentUrl ? {"screen_name": username} html = await fetchHtml(url, headers) + if html.isNil: + return Profile() + result = parseIntentProfile(html) proc getProfile*(username: string): Future[Profile] {.async.} = @@ -139,6 +142,9 @@ proc getProfile*(username: string): Future[Profile] {.async.} = url = base / profilePopupUrl ? params html = await fetchHtml(url, headers, jsonKey="html") + if html.isNil: + return Profile() + if not html.querySelector(".ProfileCard-sensitiveWarningContainer").isNil: return await getProfileFallback(username, headers) diff --git a/src/cache.nim b/src/cache.nim index b9d6fed..f1edff6 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -16,15 +16,13 @@ proc getCachedProfile*(username: string; force=false): Future[Profile] {.async.} withDb: try: result.getOne("username = ?", username) - doAssert(not result.outdated()) - except: - if result.id == 0: - result = await getProfile(username) + doAssert not result.outdated() + except AssertionError: + var profile = await getProfile(username) + profile.id = result.id + result = profile + result.update() + except KeyError: + result = await getProfile(username) + if result.username.len > 0: result.insert() - elif result.outdated(): - let - profile = await getProfile(username) - oldId = result.id - result = profile - result.id = oldId - result.update()