-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added error handling to api calls
- Loading branch information
1 parent
c90c1d0
commit 314cb67
Showing
1 changed file
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<AxiosResponse> => { | ||
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<AxiosResponse> => { | ||
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<AxiosResponse> => { | ||
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<AxiosResponse> => { | ||
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<AxiosResponse> => { | ||
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<AxiosResponse> => { | ||
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') | ||
} | ||
} |