From 314cb67b5e68ffc4887e0294bea75854da1bf189 Mon Sep 17 00:00:00 2001 From: miguel-merlin Date: Thu, 28 Mar 2024 15:33:29 -0400 Subject: [PATCH] feat: Added error handling to api calls --- src/api/lib/users.tsx | 70 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/src/api/lib/users.tsx b/src/api/lib/users.tsx index 12e94e2..9beecd3 100644 --- a/src/api/lib/users.tsx +++ b/src/api/lib/users.tsx @@ -1,27 +1,83 @@ -import { type AxiosResponse } from 'axios' +import { type AxiosResponse, AxiosError } from 'axios' import request from '../apiClient' import type { User } from '../../types/index' +const API_NAME = 'User Management' + export const getAllUsers = async (): Promise => { - return await request('GET', '/v1/users/all') + try { + const response = await request('GET', '/v1/users/all') + return response + } catch (error) { + if (error instanceof AxiosError) { + console.error(`${API_NAME} Error: ${error.message}`, error.response?.data) + throw new Error(`${API_NAME} Error: ${error.response?.status} ${error.response?.data?.error}`) + } + throw new Error('Unknown Error') + } } export const getUser = async (username: string): Promise => { - return await request('GET', `/v1/users/user?username=${username}`) + try { + const response = await request('GET', `/v1/users/user?username=${username}`) + return response + } catch (error) { + if (error instanceof AxiosError) { + console.error(`${API_NAME} Error: ${error.message}`, error.response?.data) + throw new Error(`${API_NAME} Error: ${error.response?.status} ${error.response?.data?.error}`) + } + throw new Error('Unknown Error') + } } export const addUser = async (user: User): Promise => { - return await request('POST', '/v1/users/user', JSON.stringify(user)) + try { + const response = await request('POST', '/v1/users/user', JSON.stringify(user)) + return response + } catch (error) { + if (error instanceof AxiosError) { + console.error(`${API_NAME} Error: ${error.message}`, error.response?.data) + throw new Error(`${API_NAME} Error: ${error.response?.status} ${error.response?.data?.error}`) + } + throw new Error('Unknown Error') + } } export const updateUser = async (username: string, user: User): Promise => { - return await request('PUT', `/v1/users/user?username=${username}`, JSON.stringify(user)) + try { + const response = await request('PUT', `/v1/users/user?username=${username}`, JSON.stringify(user)) + return response + } catch (error) { + if (error instanceof AxiosError) { + console.error(`${API_NAME} Error: ${error.message}`, error.response?.data) + throw new Error(`${API_NAME} Error: ${error.response?.status} ${error.response?.data?.error}`) + } + throw new Error('Unknown Error') + } } export const deleteUser = async (username: string): Promise => { - return await request('DELETE', `/v1/users/user?username=${username}`) + try { + const response = await request('DELETE', `/v1/users/user?username=${username}`) + return response + } catch (error) { + if (error instanceof AxiosError) { + console.error(`${API_NAME} Error: ${error.message}`, error.response?.data) + throw new Error(`${API_NAME} Error: ${error.response?.status} ${error.response?.data?.error}`) + } + throw new Error('Unknown Error') + } } export const getMemberSizeWeeklyChange = async (): Promise => { - return await request('GET', '/v1/users/member-size-weekly-change') + try { + const response = await request('GET', '/v1/users/memberSizeWeeklyChange') + return response + } catch (error) { + if (error instanceof AxiosError) { + console.error(`${API_NAME} Error: ${error.message}`, error.response?.data) + throw new Error(`${API_NAME} Error: ${error.response?.status} ${error.response?.data?.error}`) + } + throw new Error('Unknown Error') + } }