Skip to content

Commit

Permalink
Merge pull request #3673 from LiteFarmOrg/LF-4690b-fix-ensemble-api-c…
Browse files Browse the repository at this point in the history
…all-bug-on-token-refresh

LF-4690b Fix Ensemble API call bug on token refresh and return descriptive errors on API call failure
  • Loading branch information
antsgar authored Jan 31, 2025
2 parents bd5a678 + 1341d78 commit a3b7c6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/api/src/controllers/farmAddonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ const farmAddonController = {

return res.status(200).send();
} catch (error) {
console.log(error);
return res.status(400).json({
error,
console.error(error);
return res.status(error.status || 400).json({
error: error.message || error,
});
}
},
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/controllers/sensorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ const sensorController = {
sensor_arrays,
});
} catch (error) {
console.log(error);
return res.status(400).json({
error,
console.error(error);
return res.status(error.status || 400).json({
error: error.message || error,
});
}
},
Expand Down
26 changes: 18 additions & 8 deletions packages/api/src/util/ensemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const ENSEMBLE_UNITS_MAPPING = {
const getValidEnsembleOrg = async (org_uuid) => {
const allRegisteredOrganisations = await getEnsembleOrganisations();

const organisation = allRegisteredOrganisations.find(({ uuid }) => uuid === org_uuid);
const organisation = allRegisteredOrganisations?.find(({ uuid }) => uuid === org_uuid);

return organisation;
};
Expand All @@ -92,7 +92,7 @@ const getEnsembleSensors = async (farm_id) => {

const devices = await getOrganisationDevices(farmEnsembleAddon.org_pk);

if (!devices.length) {
if (!devices?.length) {
return { sensors: [], sensor_arrays: [] };
}

Expand Down Expand Up @@ -334,14 +334,17 @@ async function getEnsembleOrganisations() {
url: `${ensembleAPI}/organizations/`,
};
const onError = () => {
throw new Error('Unable to fetch ESCI organisation');
const err = new Error('Unable to fetch ESCI organisations');
err.status = 500;
throw err;
};

const response = await ensembleAPICall(axiosObject, onError);

return response.data;
} catch (error) {
console.log(error);
console.error(error);
throw error;
}
}

Expand All @@ -359,14 +362,17 @@ async function getOrganisationDevices(organisation_pk) {
url: `${ensembleAPI}/organizations/${organisation_pk}/devices/`,
};
const onError = () => {
throw new Error('Unable to fetch ESCI devices');
const err = new Error('Unable to fetch ESCI devices');
err.status = 500;
throw err;
};

const response = await ensembleAPICall(axiosObject, onError);

return response.data;
} catch (error) {
console.log(error);
throw error;
}
}

Expand Down Expand Up @@ -456,7 +462,7 @@ async function fetchAccessToken() {
async function refreshAndRecall(axiosObject, onError, onResponse, retries) {
const result = await refreshTokens();
if (!result?.access || !result?.refresh) return result;
return ensembleAPICall(result.access, axiosObject, onError, onResponse, retries - 1);
return ensembleAPICall(axiosObject, onError, onResponse, retries - 1);
}

/**
Expand Down Expand Up @@ -501,7 +507,9 @@ async function refreshTokens() {
if (isAuthError(error)) {
return await authenticateToGetTokens();
} else {
return { status: 500, detail: 'Failed to authenticate with Ensemble.' };
const err = new Error('Failed to authenticate with Ensemble.');
err.status = 500;
throw err;
}
}
}
Expand All @@ -524,7 +532,9 @@ async function authenticateToGetTokens() {
);
return response.data;
} catch (error) {
return { status: 500, detail: 'Failed to authenticate with Ensemble.' };
const err = new Error('Failed to authenticate with Ensemble.');
err.status = 500;
throw err;
}
}

Expand Down

0 comments on commit a3b7c6d

Please sign in to comment.