From b134213f30d46dd7f5690a38425deb4fd752148c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20SABLONNI=C3=88RE?= Date: Tue, 5 Dec 2023 14:53:03 +0100 Subject: [PATCH] fix(api): improve error message with `EAI_AGAIN` and `ECONNRESET` When the clever-tools were using the `request.superagent` from the clever-client, `EAI_AGAIN` and `ECONNRESET` errors were caught, and message were improved. When we moved to the common request.fetch, we lost improvment. This commit adds back the improved messages but in the clever-tools code. --- src/models/send-to-api.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/models/send-to-api.js b/src/models/send-to-api.js index fa2eff2..382bcfc 100644 --- a/src/models/send-to-api.js +++ b/src/models/send-to-api.js @@ -28,7 +28,19 @@ async function sendToApi (requestParams) { } return requestParams; }) - .then((requestParams) => request(requestParams, { retry: 1 })); + .then((requestParams) => request(requestParams, { retry: 1 })) + .catch(processError); +} + +function processError (error) { + const code = error.code ?? error?.cause?.code; + if (code === 'EAI_AGAIN') { + throw new Error('Cannot reach the Clever Cloud API, please check your internet connection.', { cause: error }); + } + if (code === 'ECONNRESET') { + throw new Error('The connection to the Clever Cloud API was closed abruptly, please try again.', { cause: error }); + } + throw error; } function sendToWarp10 (requestParams) { @@ -45,4 +57,4 @@ async function getHostAndTokens () { }; } -module.exports = { sendToApi, sendToWarp10, getHostAndTokens }; +module.exports = { sendToApi, sendToWarp10, getHostAndTokens, processError };