From 58c6ac6bcc233f8a9e65dc45646850fd492d4d09 Mon Sep 17 00:00:00 2001 From: Adam Moussa <166072409+amoussa1229@users.noreply.github.com> Date: Thu, 16 Apr 2026 16:32:10 -0400 Subject: [PATCH] Fix error swallowing in apiUtil and stale auth headers - Restore error propagation in all fetch methods (was commented out) - Change static header objects to functions so auth token is fresh per request - Add missing return in putFormBody - Properly throw on 401 unauthorized responses --- src/apiUtil.js | 112 ++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 72 deletions(-) diff --git a/src/apiUtil.js b/src/apiUtil.js index ecb5531b..f2604308 100644 --- a/src/apiUtil.js +++ b/src/apiUtil.js @@ -1,108 +1,76 @@ import { API_ERROR_MESSAGE } from "./constants"; import { getAccessToken } from "./tokenUtility"; -const STANDARD_HEADERS = { +const getHeaders = () => ({ Accept: "application/json", "Content-Type": "application/json", pragma: "no-cache", "cache-control": "no-cache", - Authorization: getAccessToken() -}; + Authorization: getAccessToken(), +}); -const STANDARD_HEADERS_FORM = { +const getFormHeaders = () => ({ accept: "application/json", pragma: "no-cache", "cache-control": "no-cache", Authorization: getAccessToken(), -}; +}); export const get = (url) => fetch(url, { - headers: { ...STANDARD_HEADERS, Authorization: getAccessToken() }, + headers: getHeaders(), method: "GET", - }) - .then(handleHttpResponse) - .catch((ex) => { - // throw new Error(ex.message || API_ERROR_MESSAGE); - }); + }).then(handleHttpResponse); export const post = (url, payload) => fetch(url, { - headers: STANDARD_HEADERS, + headers: getHeaders(), method: "POST", body: JSON.stringify(payload), - }) - .then(handleHttpResponse) - .catch((ex) => { - // throw new Error(ex.message || API_ERROR_MESSAGE); - }); + }).then(handleHttpResponse); - export const postFormBody = (url, payload) => { - const formData = new FormData(); - - for (const [key, value] of Object.entries(payload)) { - formData.append(key, value); - } - - return fetch(url, { - method: "POST", - headers: { - ...STANDARD_HEADERS_FORM, - }, - body: formData, - }) - .then(handleHttpResponse) - .catch((ex) => { - // throw new Error(ex.message || API_ERROR_MESSAGE); - }); - }; - -export const put = (url, payload) => - fetch(url, { - headers: STANDARD_HEADERS, - method: "PUT", - body: JSON.stringify(payload), - }) - .then(handleHttpResponse) - .catch((ex) => { - // throw new Error(ex.message || API_ERROR_MESSAGE); - }); - -export const putFormBody = (url, payload) => { +export const postFormBody = (url, payload) => { const formData = new FormData(); - for (const [key, value] of Object.entries(payload)) { formData.append(key, value); } - - fetch(url, { - method: "PUT", - headers: { - ...STANDARD_HEADERS_FORM, - }, + return fetch(url, { + method: "POST", + headers: getFormHeaders(), body: formData, - }) - .then(handleHttpResponse) - .catch((ex) => { - // throw new Error(ex.message || API_ERROR_MESSAGE); - }); + }).then(handleHttpResponse); +}; + +export const put = (url, payload) => + fetch(url, { + headers: getHeaders(), + method: "PUT", + body: JSON.stringify(payload), + }).then(handleHttpResponse); + +export const putFormBody = (url, payload) => { + const formData = new FormData(); + for (const [key, value] of Object.entries(payload)) { + formData.append(key, value); } + return fetch(url, { + method: "PUT", + headers: getFormHeaders(), + body: formData, + }).then(handleHttpResponse); +}; + export const deleteItem = (url, payload) => fetch(url, { - headers: STANDARD_HEADERS, - method: "Delete", + headers: getHeaders(), + method: "DELETE", body: JSON.stringify(payload), - }) - .then(handleHttpResponse) - .catch((ex) => { - // throw new Error(ex.message || API_ERROR_MESSAGE); - }); + }).then(handleHttpResponse); const handleHttpResponse = (response) => { - if (response.ok) - return response.json(); - else if (response.status === 401) { - //throw new Error("You are not authorized to access this page."); + if (response.ok) return response.json(); + if (response.status === 401) { + throw new Error("You are not authorized to access this page."); } throw new Error(API_ERROR_MESSAGE); };