Skip to content

Commit 25f0065

Browse files
committed
fix: make error handling more descriptive and catch JWT config error
1 parent 31cba39 commit 25f0065

File tree

6 files changed

+39
-18
lines changed

6 files changed

+39
-18
lines changed

src/ui/services/git-push.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const getPushes = async (
4545
setData,
4646
setAuth,
4747
setIsError,
48+
setErrorMessage,
4849
query = {
4950
blocked: true,
5051
canceled: false,
@@ -60,15 +61,16 @@ const getPushes = async (
6061
.then((response) => {
6162
const data = response.data;
6263
setData(data);
63-
setIsLoading(false);
6464
})
6565
.catch((error) => {
66-
setIsLoading(false);
66+
setIsError(true);
6767
if (error.response && error.response.status === 401) {
6868
setAuth(false);
69+
setErrorMessage('Failed to authorize user. If JWT auth is enabled, please check your configuration or disable it.');
6970
} else {
70-
setIsError(true);
71+
setErrorMessage(`Error fetching pushes: ${error.response.data.message}`);
7172
}
73+
}).finally(() => {
7274
setIsLoading(false);
7375
});
7476
};

src/ui/services/repo.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,31 @@ class DupUserValidationError extends Error {
3333
}
3434
}
3535

36-
const getRepos = async (setIsLoading, setData, setAuth, setIsError, query = {}) => {
36+
const getRepos = async (
37+
setIsLoading,
38+
setData,
39+
setAuth,
40+
setIsError,
41+
setErrorMessage,
42+
query = {},
43+
) => {
3744
const url = new URL(`${baseUrl}/repo`);
3845
url.search = new URLSearchParams(query);
3946
setIsLoading(true);
4047
await axios(url.toString(), config)
4148
.then((response) => {
4249
const data = response.data;
4350
setData(data);
44-
setIsLoading(false);
4551
})
4652
.catch((error) => {
47-
setIsLoading(false);
53+
setIsError(true);
4854
if (error.response && error.response.status === 401) {
4955
setAuth(false);
56+
setErrorMessage('Failed to authorize user. If JWT auth is enabled, please check your configuration or disable it.');
5057
} else {
51-
setIsError(true);
58+
setErrorMessage(`Error fetching repositories: ${error.response.data.message}`);
5259
}
60+
}).finally(() => {
5361
setIsLoading(false);
5462
});
5563
};
@@ -61,15 +69,14 @@ const getRepo = async (setIsLoading, setData, setAuth, setIsError, id) => {
6169
.then((response) => {
6270
const data = response.data;
6371
setData(data);
64-
setIsLoading(false);
6572
})
6673
.catch((error) => {
67-
setIsLoading(false);
6874
if (error.response && error.response.status === 401) {
6975
setAuth(false);
7076
} else {
7177
setIsError(true);
7278
}
79+
}).finally(() => {
7380
setIsLoading(false);
7481
});
7582
};

src/ui/services/user.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,31 @@ const getUser = async (setIsLoading, setData, setAuth, setIsError, id = null) =>
4444
});
4545
};
4646

47-
const getUsers = async (setIsLoading, setData, setAuth, setIsError, query = {}) => {
47+
const getUsers = async (
48+
setIsLoading,
49+
setData,
50+
setAuth,
51+
setIsError,
52+
setErrorMessage,
53+
query = {},
54+
) => {
4855
const url = new URL(`${baseUrl}/api/v1/user`);
4956
url.search = new URLSearchParams(query);
5057
setIsLoading(true);
5158
await axios(url.toString(), { withCredentials: true })
5259
.then((response) => {
5360
const data = response.data;
5461
setData(data);
55-
setIsLoading(false);
5662
})
5763
.catch((error) => {
58-
setIsLoading(false);
64+
setIsError(true);
5965
if (error.response && error.response.status === 401) {
6066
setAuth(false);
67+
setErrorMessage('Failed to authorize user. If JWT auth is enabled, please check your configuration or disable it.');
6168
} else {
62-
setIsError(true);
69+
setErrorMessage(`Error fetching users: ${error.response.data.message}`);
6370
}
71+
}).finally(() => {
6472
setIsLoading(false);
6573
});
6674
};

src/ui/views/OpenPushRequests/components/PushesTable.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function PushesTable(props) {
2323
const [filteredData, setFilteredData] = useState([]);
2424
const [isLoading, setIsLoading] = useState(false);
2525
const [isError, setIsError] = useState(false);
26+
const [errorMessage, setErrorMessage] = useState('');
2627
const navigate = useNavigate();
2728
const [, setAuth] = useState(true);
2829
const [currentPage, setCurrentPage] = useState(1);
@@ -35,7 +36,7 @@ export default function PushesTable(props) {
3536
for (const k in props) {
3637
if (k) query[k] = props[k];
3738
}
38-
getPushes(setIsLoading, setData, setAuth, setIsError, query);
39+
getPushes(setIsLoading, setData, setAuth, setIsError, setErrorMessage, query);
3940
}, [props]);
4041

4142
useEffect(() => {
@@ -69,7 +70,7 @@ export default function PushesTable(props) {
6970
const paginate = (pageNumber) => setCurrentPage(pageNumber);
7071

7172
if (isLoading) return <div>Loading...</div>;
72-
if (isError) return <div>Something went wrong ...</div>;
73+
if (isError) return <div>{errorMessage}</div>;
7374

7475
return (
7576
<div>

src/ui/views/RepoList/Components/Repositories.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default function Repositories(props) {
2424
const [, setAuth] = useState(true);
2525
const [isLoading, setIsLoading] = useState(false);
2626
const [isError, setIsError] = useState(false);
27+
const [errorMessage, setErrorMessage] = useState('');
2728
const [currentPage, setCurrentPage] = useState(1);
2829
const itemsPerPage = 5;
2930
const navigate = useNavigate();
@@ -44,6 +45,7 @@ export default function Repositories(props) {
4445
},
4546
setAuth,
4647
setIsError,
48+
setErrorMessage,
4749
query,
4850
);
4951
}, [props]);
@@ -99,7 +101,7 @@ export default function Repositories(props) {
99101
const paginatedData = filteredData.slice(startIdx, startIdx + itemsPerPage);
100102

101103
if (isLoading) return <div>Loading...</div>;
102-
if (isError) return <div>Something went wrong ...</div>;
104+
if (isError) return <div>{errorMessage}</div>;
103105

104106
const addrepoButton = user.admin ? (
105107
<GridItem>

src/ui/views/UserList/Components/UserList.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function UserList(props) {
2525
const [, setAuth] = useState(true);
2626
const [isLoading, setIsLoading] = useState(false);
2727
const [isError, setIsError] = useState(false);
28+
const [errorMessage, setErrorMessage] = useState('');
2829
const navigate = useNavigate();
2930
const [currentPage, setCurrentPage] = useState(1);
3031
const itemsPerPage = 5;
@@ -39,11 +40,11 @@ export default function UserList(props) {
3940
if (!k) continue;
4041
query[k] = props[k];
4142
}
42-
getUsers(setIsLoading, setData, setAuth, setIsError, query);
43+
getUsers(setIsLoading, setData, setAuth, setIsError, setErrorMessage, query);
4344
}, [props]);
4445

4546
if (isLoading) return <div>Loading...</div>;
46-
if (isError) return <div>Something went wrong...</div>;
47+
if (isError) return <div>{errorMessage}</div>;
4748

4849
const filteredUsers = data.filter(
4950
(user) =>

0 commit comments

Comments
 (0)