From 5ed035a432d4965f63fdad7ea1e66ed056871304 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 30 Jan 2021 19:40:20 +1300 Subject: [PATCH] Preserve history error kind --- src/lib/collectors.js | 14 +++++++------- src/lib/structures/RequestHistory.js | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/collectors.js b/src/lib/collectors.js index 5fea0dd..2d7da78 100644 --- a/src/lib/collectors.js +++ b/src/lib/collectors.js @@ -91,7 +91,7 @@ function fetchUserFromHTML(username) { if (history.store.has("user")) { const entry = history.store.get("user") if (!entry.lastRequestSuccessful && Date.now() < entry.lastRequestAt + blockedCacheConfig.time) { - return Promise.reject(constants.symbols.RATE_LIMITED) + return Promise.reject(entry.kind || constants.symbols.RATE_LIMITED) } } } @@ -148,7 +148,7 @@ function fetchUserFromHTML(username) { } }).catch(error => { if (error === constants.symbols.INSTAGRAM_DEMANDS_LOGIN || error === constants.symbols.RATE_LIMITED) { - history.report("user", false) + history.report("user", false, error) } throw error }) @@ -230,7 +230,7 @@ function fetchUserFromCombined(userID, username) { return {user, quotaUsed} }).catch(error => { if (error === constants.symbols.RATE_LIMITED) { - history.report("reel", false) + history.report("reel", false, error) } throw error }) @@ -277,7 +277,7 @@ function fetchTimelinePage(userID, after) { if (history.store.has("timeline")) { const entry = history.store.get("timeline") if (!entry.lastRequestSuccessful && Date.now() < entry.lastRequestAt + blockedCacheConfig.time) { - return Promise.reject(constants.symbols.RATE_LIMITED) + return Promise.reject(entry.kind || constants.symbols.RATE_LIMITED) } } } @@ -305,7 +305,7 @@ function fetchTimelinePage(userID, after) { return timeline }).catch(error => { if (error === constants.symbols.RATE_LIMITED || error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) { - history.report("timeline", false) + history.report("timeline", false, error) } throw error }) @@ -337,7 +337,7 @@ function fetchIGTVPage(userID, after) { return timeline }).catch(error => { if (error === constants.symbols.RATE_LIMITED || error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) { - history.report("igtv", false) + history.report("igtv", false, error) } throw error }) @@ -441,7 +441,7 @@ function fetchShortcodeData(shortcode) { } }).catch(error => { if (error === constants.symbols.RATE_LIMITED || error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) { - history.report("post", false) + history.report("post", false, error) } throw error }) diff --git a/src/lib/structures/RequestHistory.js b/src/lib/structures/RequestHistory.js index 0cab94e..f3c9b1f 100644 --- a/src/lib/structures/RequestHistory.js +++ b/src/lib/structures/RequestHistory.js @@ -7,7 +7,7 @@ class RequestHistory { */ constructor(tracked) { this.tracked = new Set(tracked) - /** @type {Map} */ + /** @type {Map} */ this.store = new Map() for (const key of tracked) { this.store.set(key, { @@ -20,12 +20,14 @@ class RequestHistory { /** * @param {string} key * @param {boolean} success + * @param {any} [kind] */ - report(key, success) { + report(key, success, kind) { if (!this.tracked.has(key)) throw new Error(`Trying to report key ${key}, but is not tracked`) const entry = this.store.get(key) entry.lastRequestAt = Date.now() entry.lastRequestSuccessful = success + entry.kind = kind if (constants.caching.db_request_history) { db.prepare("INSERT INTO RequestHistory (type, success, timestamp) VALUES (?, ?, ?)").run(key, +entry.lastRequestSuccessful, entry.lastRequestAt) }