Skip to content

Commit 9c3b9a0

Browse files
committed
Fix error handling and simplify showing http errors
1 parent fc90543 commit 9c3b9a0

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

resources/scripts/components/server/network/NetworkContainer.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@ import { Allocation } from '@/api/server/getServer';
1414
import Spinner from '@/components/elements/Spinner';
1515
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
1616
import useFlash from '@/plugins/useFlash';
17-
import { httpErrorToHuman } from '@/api/http';
1817

1918
const Code = styled.code`${tw`font-mono py-1 px-2 bg-neutral-900 rounded text-sm block`}`;
2019
const Label = styled.label`${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}`;
2120

2221
const NetworkContainer = () => {
2322
const server = useServer();
24-
const { clearFlashes, clearAndAddError } = useFlash();
23+
const { clearFlashes, clearAndAddHttpError } = useFlash();
2524
const { data, error, mutate } = useSWR<Allocation[]>(server.uuid, key => getServerAllocations(key), { initialData: server.allocations });
2625

2726
const setPrimaryAllocation = (ip: string, port: number) => {
2827
clearFlashes('server:network');
2928

30-
mutate(data?.map(a => (a.ip === ip && a.port === port) ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
29+
mutate(data?.map(a => (a.ip === ip && a.port === port) ? { ...a, isDefault: true } : {
30+
...a,
31+
isDefault: false,
32+
}), false);
3133

3234
setPrimaryServerAllocation(server.uuid, ip, port)
33-
.catch(error => clearAndAddError({ key: 'server:network', message: httpErrorToHuman(error) }));
35+
.catch(error => clearAndAddHttpError({ key: 'server:network', error }));
3436
};
3537

3638
useEffect(() => {
3739
if (error) {
38-
clearAndAddError({ key: 'server:network', message: error });
40+
clearAndAddHttpError({ key: 'server:network', error });
3941
}
4042
}, [ error ]);
4143

resources/scripts/state/flashes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Action, action } from 'easy-peasy';
22
import { FlashMessageType } from '@/components/MessageBox';
3+
import { httpErrorToHuman } from '@/api/http';
34

45
export interface FlashStore {
56
items: FlashMessage[];
67
addFlash: Action<FlashStore, FlashMessage>;
78
addError: Action<FlashStore, { message: string; key?: string }>;
8-
clearAndAddError: Action<FlashStore, { message: string, key: string }>;
9+
clearAndAddHttpError: Action<FlashStore, { error: any, key: string }>;
910
clearFlashes: Action<FlashStore, string | void>;
1011
}
1112

@@ -28,8 +29,8 @@ const flashes: FlashStore = {
2829
state.items.push({ type: 'error', title: 'Error', ...payload });
2930
}),
3031

31-
clearAndAddError: action((state, payload) => {
32-
state.items = [ { type: 'error', title: 'Error', ...payload } ];
32+
clearAndAddHttpError: action((state, { key, error }) => {
33+
state.items = [ { type: 'error', title: 'Error', key, message: httpErrorToHuman(error) } ];
3334
}),
3435

3536
clearFlashes: action((state, payload) => {

0 commit comments

Comments
 (0)