Show better error messages
This commit is contained in:
@@ -1,22 +1,30 @@
|
||||
import { RaidGroup } from "@/interface/RaidGroup";
|
||||
import { api } from "@/util/AxiosUtil";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
|
||||
export function useGetRaidGroup(raidGroupId: string, disabled: boolean){
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", raidGroupId],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/${raidGroupId}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup/${raidGroupId}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid group");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data?.raidGroupId ? response.data as RaidGroup : undefined;
|
||||
return response.data?.raidGroupId ? response.data as RaidGroup : undefined;
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled: !disabled
|
||||
});
|
||||
@@ -33,16 +41,23 @@ export function useGetRaidGroups(page: number, pageSize: number, searchTerm?: st
|
||||
params.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
const response = await api.get(`/raidGroup?${params}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup?${params}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as RaidGroup[];
|
||||
return response.data as RaidGroup[];
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -57,16 +72,23 @@ export function useGetRaidGroupsCount(searchTerm?: string){
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/count?${searchParams}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup/count?${searchParams}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups count");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
return response.data.count as number;
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -82,16 +104,23 @@ export function useGetRaidGroupsByGame(gameId: string, page: number, pageSize: n
|
||||
params.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
const response = await api.get(`/raidGroup/game/${gameId}?${params}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup/game/${gameId}?${params}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as RaidGroup[];
|
||||
return response.data as RaidGroup[];
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -106,16 +135,23 @@ export function useGetRaidGroupsByGameCount(gameId: string, searchTerm?: string)
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", gameId, "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/game/${gameId}/count?${searchParams}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup/game/${gameId}/count?${searchParams}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups count");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
return response.data.count as number;
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -131,16 +167,23 @@ export function useGetRaidGroupsByAccount(accountId: string, page: number, pageS
|
||||
params.append("searchTerm", searchTerm);
|
||||
}
|
||||
|
||||
const response = await api.get(`/raidGroup/account/${accountId}?${params}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup/account/${accountId}?${params}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data as RaidGroup[];
|
||||
return response.data as RaidGroup[];
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -155,16 +198,23 @@ export function useGetRaidGroupsCountByAccount(accountId: string, searchTerm?: s
|
||||
return useQuery({
|
||||
queryKey: ["raidGroups", accountId, "count", searchTerm],
|
||||
queryFn: async () => {
|
||||
const response = await api.get(`/raidGroup/account/${accountId}/count?${searchParams}`);
|
||||
try{
|
||||
const response = await api.get(`/raidGroup/account/${accountId}/count?${searchParams}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to get raid groups count");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
|
||||
return response.data.count as number;
|
||||
return response.data.count as number;
|
||||
}
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -185,16 +235,23 @@ export function useCreateRaidGroup(){
|
||||
formData.append("raidGroupName", raidGroupName);
|
||||
formData.append("gameId", gameId);
|
||||
|
||||
const response = await api.post(
|
||||
"/raidGroup",
|
||||
formData
|
||||
);
|
||||
try{
|
||||
const response = await api.post(
|
||||
"/raidGroup",
|
||||
formData
|
||||
);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to create raid group");
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
@@ -220,13 +277,20 @@ export function useUpdateRaidGroup(){
|
||||
formData.append("raidGroupIcon", raidGroup.raidGroupIcon);
|
||||
}
|
||||
|
||||
const response = await api.put(`/raidGroup/${raidGroup.raidGroupId}`, formData);
|
||||
try{
|
||||
const response = await api.put(`/raidGroup/${raidGroup.raidGroupId}`, formData);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to update raid group");
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
@@ -242,13 +306,20 @@ export function useDeleteRaidGroup(){
|
||||
return useMutation({
|
||||
mutationKey: ["deleteRaidGroup"],
|
||||
mutationFn: async (raidGroupId: string) => {
|
||||
const response = await api.delete(`/raidGroup/${raidGroupId}`);
|
||||
try{
|
||||
const response = await api.delete(`/raidGroup/${raidGroupId}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to delete raid group");
|
||||
if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
catch(error){
|
||||
if(error instanceof AxiosError && error.response?.data.errors){
|
||||
throw new Error(error.response?.data.errors.join(", "));
|
||||
}
|
||||
else{
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
|
||||
Reference in New Issue
Block a user