From bbaf3ea4b63cd80b44be3cef0012ed8326ba4faf Mon Sep 17 00:00:00 2001 From: idinium96 <47635037+idinium96@users.noreply.github.com> Date: Mon, 30 May 2022 14:53:48 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=83=20handle=20error=20on=20delete=20a?= =?UTF-8?q?rchived=20listing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 64c3bc0..5c452eb 100644 --- a/index.js +++ b/index.js @@ -994,30 +994,46 @@ class ListingManager { axios(options) .then(response => { - const body = response.data; - // This return nothing (empty body) - this.emit('deleteArchivedListingSuccessful', true); + if (response?.status === 200) { + this.emit('deleteArchivedListingSuccessful', true); - // Update cached listings - this.listings = this.listings.filter(listing => listing.id === listingId); + // Update cached listings + this.listings = this.listings.filter(listing => listing.id === listingId); + } }) .catch(err => { - if (err) { - if (this.deleteArchivedFailedAttempt[listingId] === undefined) { - this.deleteArchivedFailedAttempt[listingId] = 1; - } else { - this.deleteArchivedFailedAttempt[listingId] = this.deleteArchivedFailedAttempt[listingId]++; - } + if (err.response?.status === 200) { + // Got error, but status is 200 (OK), consider success. + this.emit('deleteArchivedListingSuccessful', true); + + // Update cached listings + this.listings = this.listings.filter(listing => listing.id === listingId); + } else { + if (err.response?.status !== 404) { + // We only retry if status is not 404 (Not Found) + if (this.deleteArchivedFailedAttempt[listingId] === undefined) { + this.deleteArchivedFailedAttempt[listingId] = 1; + } else { + this.deleteArchivedFailedAttempt[listingId] = this.deleteArchivedFailedAttempt[listingId]++; + } - this.checkDeleteArchivedFailedAttempt(listingId); + this.checkDeleteArchivedFailedAttempt(listingId); - this.emit('deleteArchivedListingError', { - error: err?.name, - message: err?.message, - statusCode: err?.statusCode - }); + this.emit('deleteArchivedListingError', { + error: err?.name, + message: err?.message, + statusCode: err?.response?.status + }); + } else { + if (this.deleteArchivedFailedAttempt[listingId] !== undefined) { + delete this.deleteArchivedFailedAttempt[listingId]; + } + + // Listing not found, update cached listings + this.listings = this.listings.filter(listing => listing.id === listingId); + } } }); }