From 1db80c9ea854b956c4830dc9200d40f5758c4d91 Mon Sep 17 00:00:00 2001 From: ITurres Date: Fri, 8 Mar 2024 21:44:19 -0300 Subject: [PATCH] Refactor: 'getLikes' async function in 'involvementAPI.ts' - Removed unnecessary try-catch block in the `getLikes` function. Instead, if the response is not ok, the function now throws an error class with the response `statusText` as the message. This refactor improves the clarity and efficiency of error handling. --- src/services/involvementAPI/involvementAPI.ts | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/services/involvementAPI/involvementAPI.ts b/src/services/involvementAPI/involvementAPI.ts index 0d70db0..78be27b 100644 --- a/src/services/involvementAPI/involvementAPI.ts +++ b/src/services/involvementAPI/involvementAPI.ts @@ -22,25 +22,23 @@ const postLike = async (itemId: string) => { }; const getLikes = async () => { - try { - const response = await fetch(`${baseURL}${appId}/likes/`, { - method: 'GET', - }); - - if (!response.ok) return []; + const response = await fetch(`${baseURL}${appId}/likes/`, { + method: 'GET', + }); - // * Only parse the response if it's JSON. - // * specially when the involvementAPI : 'appId' is new, the response to be parsed wont be JSON. - const contentType = response.headers.get('Content-Type'); - if (contentType && contentType.includes('application/json')) { - const likes = await response.json(); - if (likes instanceof Array) return likes; - } + if (!response.ok) { + throw new Error(`Error fetching likes: ${response.statusText}`); + } - return []; - } catch (error) { - return error; + // * Only parse the response if it's JSON. + // * specially when the involvementAPI : 'appId' is new, the response to be parsed wont be JSON. + const contentType = response.headers.get('Content-Type'); + if (contentType && contentType.includes('application/json')) { + const likes = await response.json(); + if (likes instanceof Array) return likes; } + + return []; }; const involvement = {