From e77dacc04ee3fe95cdf826f2230610ee1c2546a1 Mon Sep 17 00:00:00 2001 From: Clara Youdale Date: Wed, 18 Dec 2024 14:30:08 -0300 Subject: [PATCH] Enhance axios instance configuration with timeout and improved error handling for redirects --- .../netlify/functions/parseNews.ts | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/site/gatsby-site/netlify/functions/parseNews.ts b/site/gatsby-site/netlify/functions/parseNews.ts index eb5b049b22..f4602fd1ab 100644 --- a/site/gatsby-site/netlify/functions/parseNews.ts +++ b/site/gatsby-site/netlify/functions/parseNews.ts @@ -77,17 +77,21 @@ const getArticle = async (url, config) => { }; const getHtmlWithCookies = async (url) => { - const axiosInstance = axios.create(); - - axiosInstance.defaults.maxRedirects = 0; - axiosInstance.defaults.withCredentials = true; - axiosInstance.defaults.credentials = 'same-origin'; + const axiosInstance = axios.create({ + timeout: 10000, // 10 seconds timeout + maxRedirects: 0, // Prevent automatic redirects + withCredentials: true, // Send cookies with request + }); axiosInstance.interceptors.response.use( (response) => response, (error) => { - console.log('Intercepted error', error.message, error.response); + console.log('Intercepted error', error.message); + + // Handle HTTP 3xx Redirects with Cookies if (error.response && [301, 302].includes(error.response.status)) { + console.log('Handling redirect', error.response.status); + const redirectUrl = error.response.headers.location; const Cookie = error.response.headers['set-cookie'] @@ -96,11 +100,20 @@ const getHtmlWithCookies = async (url) => { return axiosInstance.get(redirectUrl, { headers: { Cookie } }); } + return Promise.reject(error); } ); - const response = await axiosInstance.get(url); - - return response.data; + try { + const response = await axiosInstance.get(url); + return response.data; + } catch (error) { + if (error.code === 'ECONNABORTED') { + console.error('Request timed out', error.message); + } else { + console.error('Request failed', error.message); + } + throw error; + } };