From 05e69e6663e71a22e60e94b7e545565e7bb513d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Fri, 26 Feb 2021 13:46:56 +0100 Subject: [PATCH] fix: not crash if addon http post throws (#738) added try/catch logic around fetchRetry function so that we do not crash if addon fetch call throws --- src/lib/addons/addon.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/lib/addons/addon.js b/src/lib/addons/addon.js index 21b22619f1..04c9285f66 100644 --- a/src/lib/addons/addon.js +++ b/src/lib/addons/addon.js @@ -28,16 +28,36 @@ class Addon { async fetchRetry(url, options = {}, retries = 1, backoff = 300) { const retryCodes = [408, 500, 502, 503, 504, 522, 524]; - const res = await fetch(url, options); - if (res.ok) { + try { + const res = await fetch(url, options); + if (res.ok) { + return res; + } + if (retries > 0 && retryCodes.includes(res.status)) { + setTimeout(() => { + return this.fetchRetry( + url, + options, + retries - 1, + backoff * 2, + ); + }, backoff); + } return res; + } catch (error) { + this.logger.warn(error); + if (retries > 0) { + setTimeout(() => { + return this.fetchRetry( + url, + options, + retries - 1, + backoff * 2, + ); + }, backoff); + } + return { status: 500 }; } - if (retries > 0 && retryCodes.includes(res.status)) { - setTimeout(() => { - return this.fetchRetry(url, options, retries - 1, backoff * 2); - }, backoff); - } - return res; } }