From f1687ef305fc4262e0eee0693ea46adcd8d1d0a9 Mon Sep 17 00:00:00 2001 From: PurpleMagick Date: Sat, 4 Jul 2026 10:12:42 +0300 Subject: [PATCH 1/2] #373: error handling for CopyPastor returning errors --- dist/AdvancedFlagging.user.js | 9 ++++++++- src/UserscriptTools/CopyPastorAPI.ts | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dist/AdvancedFlagging.user.js b/dist/AdvancedFlagging.user.js index 406c0d4..9711375 100644 --- a/dist/AdvancedFlagging.user.js +++ b/dist/AdvancedFlagging.user.js @@ -1176,7 +1176,14 @@ method: "GET", url, timeout: 1500, - onload: ({ responseText }) => { + onload: ({ responseText, status, statusText }) => { + if (status !== 200) { + reject(`Bad response from ${this.server}. +Status: ${status} ${statusText}. +Response was: +${responseText}`); + return; + } const response = JSON.parse(responseText); if (response.status === "failure") return; response.posts.forEach((item) => { diff --git a/src/UserscriptTools/CopyPastorAPI.ts b/src/UserscriptTools/CopyPastorAPI.ts index edceb8a..c603279 100644 --- a/src/UserscriptTools/CopyPastorAPI.ts +++ b/src/UserscriptTools/CopyPastorAPI.ts @@ -73,7 +73,12 @@ export class CopyPastorAPI extends Reporter { method: 'GET', url, timeout: 1500, - onload: ({ responseText }) => { + onload: ({ responseText, status, statusText }) => { + if (status !== 200) { + reject(`Bad response from ${this.server}.\nStatus: ${status} ${statusText}.\nResponse was:\n${responseText}`); + return; + } + const response = JSON.parse(responseText) as CopyPastorFindTargetResponse; if (response.status === 'failure') return; From a6a58232c69a37417505a72615e2d8977148683e Mon Sep 17 00:00:00 2001 From: PurpleMagick Date: Sat, 4 Jul 2026 12:26:14 +0300 Subject: [PATCH 2/2] Refactored the Natty code slightly annd added error handling for when requests fail. --- dist/AdvancedFlagging.user.js | 25 +++++++++++++++++++------ src/UserscriptTools/NattyApi.ts | 27 +++++++++++++++++++-------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/dist/AdvancedFlagging.user.js b/dist/AdvancedFlagging.user.js index 9711375..467aa90 100644 --- a/dist/AdvancedFlagging.user.js +++ b/dist/AdvancedFlagging.user.js @@ -1685,18 +1685,31 @@ ${responseText}`); chat = new ChatApi(); feedbackMessage; reportMessage; - static getAllNattyIds(ids) { + static async getAllNattyIds(ids) { const postIds2 = (ids ?? page.getAllPostIds(false, false)).join(","); - if (!Page.isStackOverflow || !postIds2) return Promise.resolve(); + if (!Page.isStackOverflow || !postIds2) return; + try { + this.nattyIds = await this.requestNattyIds(postIds2); + } catch (error) { + displayToaster("Could not connect to Natty.", "danger"); + console.error(error); + } + } + static requestNattyIds(postIds2) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: "GET", url: `${nattyFeedbackUrl}${postIds2}`, - onload: ({ status, responseText }) => { - if (status !== 200) reject(); + onload: ({ status, responseText, statusText }) => { + if (status !== 200) { + reject(`Bad response from ${nattyFeedbackUrl}. +Status: ${status} ${statusText}. +Response was: +${responseText}`); + return; + } const result = JSON.parse(responseText); - this.nattyIds = result.items.map(({ name }) => Number(name)); - resolve(); + resolve(result.items.map(({ name }) => Number(name))); }, onerror: () => reject() }); diff --git a/src/UserscriptTools/NattyApi.ts b/src/UserscriptTools/NattyApi.ts index e4cd93d..313d248 100644 --- a/src/UserscriptTools/NattyApi.ts +++ b/src/UserscriptTools/NattyApi.ts @@ -1,6 +1,6 @@ import { ChatApi } from './ChatApi'; import { AllFeedbacks } from '../shared'; -import { page } from '../AdvancedFlagging'; +import { displayToaster, page } from '../AdvancedFlagging'; import WebsocketUtils from './WebsocketUtils'; import Reporter from './Reporter'; @@ -41,22 +41,33 @@ export class NattyAPI extends Reporter { this.reportMessage = `@Natty report https://stackoverflow.com/a/${this.id}`; } - public static getAllNattyIds(ids?: number[]): Promise { + public static async getAllNattyIds(ids?: number[]): Promise { const postIds = (ids ?? page.getAllPostIds(false, false)).join(','); - if (!Page.isStackOverflow || !postIds) return Promise.resolve(); + if (!Page.isStackOverflow || !postIds) return; - return new Promise((resolve, reject) => { + try { + this.nattyIds = await this.requestNattyIds(postIds); + } catch (error) { + displayToaster('Could not connect to Natty.', 'danger'); + console.error(error); + } + } + + private static requestNattyIds(postIds: string): Promise { + return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: 'GET', url: `${nattyFeedbackUrl}${postIds}`, - onload: ({ status, responseText }) => { - if (status !== 200) reject(); + onload: ({ status, responseText, statusText }) => { + if (status !== 200) { + reject(`Bad response from ${nattyFeedbackUrl}.\nStatus: ${status} ${statusText}.\nResponse was:\n${responseText}`); + return; + } const result = JSON.parse(responseText) as NattyFeedback; - this.nattyIds = result.items.map(({ name }) => Number(name)); - resolve(); + resolve(result.items.map(({ name }) => Number(name))); }, onerror: () => reject() });