Skip to content

Commit

Permalink
feat: Added error handling to api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-merlin committed Mar 28, 2024
1 parent c90c1d0 commit 314cb67
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions src/api/lib/users.tsx
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')
}
}

0 comments on commit 314cb67

Please sign in to comment.