Skip to content

Commit 604720e

Browse files
committed
feat: add error routing
1 parent 6be464c commit 604720e

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

frontend/app/main/main.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ export default function Main() {
7171
}
7272

7373
const fetchSearchLocation = async (jwtToken: string) => {
74-
const result = await fetchGasStations(jwtToken, controller, globalUserDataState);
74+
const result = await fetchGasStations(navigate, jwtToken, controller, globalUserDataState);
7575
const myResult = result.filter(value => value?.GasPrices?.length > 0).map(value => {
7676
return value;
7777
}).map(value => ({
7878
location: value.Place + ' ' + value.Brand + ' ' + value.Street + ' ' + value.HouseNumber, e5: value.GasPrices[0].E5,
7979
e10: value.GasPrices[0].E10, diesel: value.GasPrices[0].Diesel, date: new Date(Date.parse(value.GasPrices[0].Date)), longitude: value.Longitude, latitude: value.Latitude
8080
} as TableDataRow));
8181
const myPostcode = formatPostCode(globalUserDataState.PostCode);
82-
const myJson = await fetchPriceAvgs(jwtToken, controller, myPostcode);
82+
const myJson = await fetchPriceAvgs(navigate, jwtToken, controller, myPostcode);
8383
const rowCounty = ({
8484
location: myJson.County, e5: Math.round(myJson.CountyAvgE5), e10: Math.round(myJson.CountyAvgE10), diesel: Math.round(myJson.CountyAvgDiesel), date: new Date(), longitude: 0, latitude: 0
8585
} as TableDataRow);
@@ -93,7 +93,7 @@ export default function Main() {
9393
}
9494

9595
const fetchLastMatches = async (jwtToken: string) => {
96-
const myResult = await fetchUserNotifications(jwtToken, controller, globalUserUuidState);
96+
const myResult = await fetchUserNotifications(navigate, jwtToken, controller, globalUserUuidState);
9797
//console.log(myJson);
9898
const result2 = myResult?.map(value => {
9999
//console.log(JSON.parse(value?.DataJson));
@@ -108,7 +108,7 @@ export default function Main() {
108108
setRows(result2);
109109
//const result
110110
const myPostcode = formatPostCode(globalUserDataState.PostCode);
111-
const myJson1 = await fetchTimeSlots(jwtToken, controller, myPostcode);
111+
const myJson1 = await fetchTimeSlots(navigate, jwtToken, controller, myPostcode);
112112
const timeSlots = [] as TimeSlot[];
113113
timeSlots.push(...myJson1.filter(myValue => myValue.AvgDiesel > 10).map(myValue => {
114114
const dieselTimeSlot = { x: '00.00', diesel: 0, e10: 0, e5: 0 } as TimeSlot;

frontend/app/service/http-client.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,70 @@ import { type PostCodeLocation } from "../model/location";
1717
import { type TimeSlotResponse } from "../model/time-slot-response";
1818
import { type UserRequest, type UserResponse } from "../model/user";
1919
import { type Notification } from "../model/notification";
20+
import type { NavigateFunction } from "react-router";
2021

2122
const apiPrefix = '/api';
2223

23-
const fetchGasStations = async function (jwtToken: string, controller: AbortController | null, globalUserDataState: UserDataState): Promise<GasStation[]> {
24+
async function handleResponse<T>(response: Response,navigate: NavigateFunction): Promise<T> {
25+
if (!response.ok) {
26+
const error = await response.text();
27+
if(navigate) navigate('/');
28+
throw new Error(error || `HTTP error! status: ${response.status}`);
29+
}
30+
return response.json();
31+
}
32+
33+
const fetchGasStations = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, globalUserDataState: UserDataState): Promise<GasStation[]> {
2434
const requestOptions2 = {
2535
method: 'POST',
2636
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
2737
body: JSON.stringify({ Longitude: globalUserDataState.Longitude, Latitude: globalUserDataState.Latitude, Radius: globalUserDataState.SearchRadius }),
2838
signal: controller?.signal
2939
}
3040
const result = await fetch(`${apiPrefix}/gasstation/search/location`, requestOptions2);
31-
const myResult = result.json() as Promise<GasStation[]>;
32-
return myResult;
41+
return handleResponse<GasStation[]>(result,navigate);
3342
};
3443

35-
const fetchPriceAvgs = async function (jwtToken: string, controller: AbortController | null, myPostcode: string): Promise<GasPriceAvgs> {
44+
const fetchPriceAvgs = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, myPostcode: string): Promise<GasPriceAvgs> {
3645
const requestOptions3 = {
3746
method: 'GET',
3847
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
3948
signal: controller?.signal
4049
}
4150
const result = await fetch(`${apiPrefix}/gasprice/avgs/${myPostcode}`, requestOptions3);
42-
return result.json() as Promise<GasPriceAvgs>;
51+
return handleResponse<GasPriceAvgs>(result,navigate);
4352
}
4453

45-
const fetchUserNotifications = async function (jwtToken: string, controller: AbortController | null, globalUserUuidState: string): Promise<Notification[]> {
54+
const fetchUserNotifications = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, globalUserUuidState: string): Promise<Notification[]> {
4655
const requestOptions1 = {
4756
method: 'GET',
4857
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
4958
signal: controller?.signal
5059
}
5160
const result = await fetch(`${apiPrefix}/usernotification/current/${globalUserUuidState}`, requestOptions1);
52-
return result.json() as Promise<Notification[]>;
61+
return handleResponse<Notification[]>(result,navigate);
5362
}
5463

55-
const fetchTimeSlots = async function (jwtToken: string, controller: AbortController | null, myPostcode: string): Promise<TimeSlotResponse[]> {
64+
const fetchTimeSlots = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, myPostcode: string): Promise<TimeSlotResponse[]> {
5665
const requestOptions2 = {
5766
method: 'GET',
5867
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
5968
signal: controller?.signal
6069
}
6170
const result = await fetch(`${apiPrefix}/postcode/countytimeslots/${myPostcode}`, requestOptions2);
62-
const myResult = result.json() as Promise<TimeSlotResponse[]>;
63-
return myResult;
71+
return handleResponse<TimeSlotResponse[]>(result,navigate);
6472
}
6573

6674
const postLogin = async function (userName: string, password1: string, controller: AbortController | null): Promise<UserResponse> {
6775
const requestOptions = loginSigninOptions(userName, password1, controller);
6876
const result = await fetch(`${apiPrefix}/appuser/login`, requestOptions);
69-
return result.json() as Promise<UserResponse>;
77+
return handleResponse<UserResponse>(result, null as unknown as NavigateFunction);
7078
}
7179

7280
const postSignin = async function (userName: string, password1: string, controller: AbortController | null): Promise<UserResponse> {
7381
const requestOptions = loginSigninOptions(userName, password1, controller);
7482
const result = await fetch(`${apiPrefix}/appuser/signin`, requestOptions);
75-
return result.json() as Promise<UserResponse>;
83+
return handleResponse<UserResponse>(result, null as unknown as NavigateFunction);
7684
}
7785

7886
const loginSigninOptions = (userName: string, password1: string, controller: AbortController | null) => {
@@ -84,39 +92,36 @@ const loginSigninOptions = (userName: string, password1: string, controller: Abo
8492
};
8593
};
8694

87-
const postLocationRadius = async function (jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> {
95+
const postLocationRadius = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> {
8896
const requestOptions = {
8997
method: 'POST',
9098
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
9199
body: requestString,
92100
signal: controller?.signal
93101
};
94102
const response = await fetch(`${apiPrefix}/appuser/locationradius`, requestOptions);
95-
const userResponse = response.json() as UserResponse;
96-
return userResponse;
103+
return handleResponse<UserResponse>(response,navigate);
97104
}
98105

99-
const fetchLocation = async function (jwtToken: string, controller: AbortController | null, location: string): Promise<PostCodeLocation[]> {
106+
const fetchLocation = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, location: string): Promise<PostCodeLocation[]> {
100107
const requestOptions = {
101108
method: 'GET',
102109
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
103110
signal: controller?.signal
104111
};
105112
const response = await fetch(`${apiPrefix}/appuser/location?location=${location}`, requestOptions);
106-
const locations = response.json() as Promise<PostCodeLocation[]>;
107-
return locations;
113+
return handleResponse<PostCodeLocation[]>(response,navigate);
108114
}
109115

110-
const postTargetPrices = async function (jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> {
116+
const postTargetPrices = async function (navigate: NavigateFunction, jwtToken: string, controller: AbortController | null, requestString: string): Promise<UserResponse> {
111117
const requestOptions = {
112118
method: 'POST',
113119
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` },
114120
body: requestString,
115121
signal: controller?.signal
116122
};
117123
const response = await fetch(`${apiPrefix}/appuser/targetprices`, requestOptions);
118-
const result = response.json() as Promise<UserResponse>;
119-
return result;
124+
return handleResponse<UserResponse>(response,navigate);
120125
}
121126

122127
export { fetchGasStations };

frontend/app/target-price-modal/target-price-modal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import { postTargetPrices } from "../service/http-client";
1818
import { type UserRequest } from "../model/user";
1919
import { useAtom } from 'jotai';
2020
import { useTranslation } from 'node_modules/react-i18next';
21+
import { useNavigate } from 'react-router';
2122

2223

2324
const TargetPriceModal = () => {
2425
let controller: AbortController | null = null;
2526
const { t } = useTranslation();
27+
const navigate = useNavigate();
2628
const [targetDiesel, setTargetDiesel] = useState('0');
2729
const [targetE5, setTargetE5] = useState('0');
2830
const [targetE10, setTargetE10] = useState('0');
@@ -71,7 +73,7 @@ const TargetPriceModal = () => {
7173
const myE10 = updatePrice(targetE10);
7274
controller = new AbortController();
7375
const requestString = JSON.stringify({ Username: globalUserNameState, Password: '', TargetDiesel: myDiesel, TargetE10: myE10, TargetE5: myE5 } as UserRequest);
74-
const result = await postTargetPrices(globalJwtTokenState, controller, requestString);
76+
const result = await postTargetPrices(navigate, globalJwtTokenState, controller, requestString);
7577
controller = null;
7678
setGlobalUserDataState({
7779
Latitude: globalUserDataState.Latitude, Longitude: globalUserDataState.Longitude, SearchRadius: globalUserDataState.SearchRadius, PostCode: globalUserDataState.PostCode,

0 commit comments

Comments
 (0)