Handle api error json (#18)

* Handle api error json

* Fix get vehicles error handling
Update .env.template
This commit is contained in:
John Wu
2021-03-17 15:11:41 -07:00
committed by GitHub
parent 2e1f4a7a7c
commit bf401e68eb
10 changed files with 69 additions and 47 deletions

View File

@@ -24,10 +24,13 @@ export const VehicleProvider = ({ children }) => {
const getVehicles = async (search, token) => {
try {
setBusy(true);
const {
data: { data },
} = await api.getVehicles(search, token);
setVehicles(data);
const result = await api.getVehicles(search, token);
if (result.error) {
setVehicles([]);
throw new Error(`Get vehicles error. ${result.message}`);
} else {
setVehicles(result.data);
}
} finally {
setBusy(false);
}
@@ -37,7 +40,9 @@ export const VehicleProvider = ({ children }) => {
try {
setBusy(true);
validateAdd(vehicle);
await api.addVehicle(vehicle, token);
const result = await api.addVehicle(vehicle, token);
if (result.error) throw new Error(`Add vehicle error. ${result.message}`);
return result;
} finally {
setBusy(false);
}