From ebe7e49a64b298386945e28aad2c0ce5affedff9 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <p.gawron@atcomp.pl>
Date: Wed, 2 Oct 2024 08:56:44 +0200
Subject: [PATCH] provide map for source files and source error to axios
 stacktrace

---
 next.config.js                          |  1 +
 src/services/api/utils/axiosInstance.ts | 36 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/next.config.js b/next.config.js
index 058121d9..46540574 100644
--- a/next.config.js
+++ b/next.config.js
@@ -3,6 +3,7 @@ const nextConfig = {
   reactStrictMode: true,
   basePath: process.env.APP_PREFIX ? process.env.APP_PREFIX + '/index.html' : '',
   assetPrefix: process.env.APP_PREFIX ? process.env.APP_PREFIX : '',
+  productionBrowserSourceMaps: true,
   output: 'export',
   images: {
     unoptimized: true,
diff --git a/src/services/api/utils/axiosInstance.ts b/src/services/api/utils/axiosInstance.ts
index aaa17d28..7f39522e 100644
--- a/src/services/api/utils/axiosInstance.ts
+++ b/src/services/api/utils/axiosInstance.ts
@@ -8,3 +8,39 @@ export const axiosInstance = axios.create({
 export const axiosInstanceNewAPI = axios.create({
   baseURL: BASE_NEW_API_URL,
 });
+
+axiosInstance.interceptors.request.use(config => {
+  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+  // @ts-expect-error
+  // eslint-disable-next-line no-param-reassign
+  config.errorContext = new Error('Thrown at:');
+  return config;
+});
+
+axiosInstance.interceptors.response.use(undefined, async error => {
+  const originalStackTrace = error.config?.errorContext?.stack;
+  if (originalStackTrace) {
+    // eslint-disable-next-line no-param-reassign
+    error.stack = `${error.stack}\n${originalStackTrace}`;
+  }
+
+  throw error;
+});
+
+axiosInstanceNewAPI.interceptors.request.use(config => {
+  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+  // @ts-expect-error
+  // eslint-disable-next-line no-param-reassign
+  config.errorContext = new Error('Thrown at:');
+  return config;
+});
+
+axiosInstanceNewAPI.interceptors.response.use(undefined, async error => {
+  const originalStackTrace = error.config?.errorContext?.stack;
+  if (originalStackTrace) {
+    // eslint-disable-next-line no-param-reassign
+    error.stack = `${error.stack}\n${originalStackTrace}`;
+  }
+
+  throw error;
+});
-- 
GitLab