improvement: error handeling structure changed and unified all across the app. user no longer sees inappropriate messages.

This commit is contained in:
2026-07-12 18:27:38 +03:30
parent 901d838a2c
commit fab5111aa8
45 changed files with 977 additions and 241 deletions

View File

@@ -22,7 +22,7 @@ import { treatmentCatalogApi } from '@/lib/api/treatment-catalog';
import { treatmentsApi } from '@/lib/api/treatments';
import { pickAutoAppointment } from '@/components/shared/treatmentSelection';
import { canEditTreatment, canViewTreatment } from '@/components/shared/permissions';
import { formatApiErrorMessage } from '@/components/shared/formatApiError';
import { getUserFacingError } from '@/components/shared/formatApiError';
import { useToast } from '@/lib/hooks/useToast';
import type { Organization } from '@/types/organization';
import type { AppointmentRecord } from '@/types/appointment';
@@ -264,6 +264,7 @@ export function TreatmentWorkspace({
initialAppointmentId = null,
}: TreatmentWorkspaceProps) {
const t = useTranslations('treatment');
const tErrors = useTranslations('errors');
const router = useRouter();
const { showError, showSuccess, messages: toastMessages } = useToast();
const canView = canViewTreatment(currentOrganization);
@@ -493,7 +494,7 @@ export function TreatmentWorkspace({
}
} catch (error: unknown) {
if (!cancelled) {
showError(formatApiErrorMessage(error, t('errorLoadAppointments')));
showError(getUserFacingError(error, tErrors, t('errorLoadAppointments')));
}
} finally {
if (!cancelled) setApptsLoading(false);
@@ -534,7 +535,7 @@ export function TreatmentWorkspace({
);
} catch (error: unknown) {
if (!cancelled) {
showError(formatApiErrorMessage(error, t('errorLoadOrgs')));
showError(getUserFacingError(error, tErrors, t('errorLoadOrgs')));
}
}
})();
@@ -563,7 +564,7 @@ export function TreatmentWorkspace({
if (!cancelled) setHistory(response.data);
} catch (error: unknown) {
if (!cancelled) {
showError(formatApiErrorMessage(error, t('errorLoadHistory')));
showError(getUserFacingError(error, tErrors, t('errorLoadHistory')));
}
} finally {
if (!cancelled) setHistoryLoading(false);
@@ -621,7 +622,7 @@ export function TreatmentWorkspace({
setSaveStatus('idle');
} catch (error: unknown) {
if (!cancelled) {
showError(formatApiErrorMessage(error, t('errorLoadDraft')));
showError(getUserFacingError(error, tErrors, t('errorLoadDraft')));
}
} finally {
if (!cancelled) {
@@ -693,7 +694,7 @@ export function TreatmentWorkspace({
setSaveStatus('saved');
} catch (error: unknown) {
setSaveStatus('error');
showError(formatApiErrorMessage(error, t('errorSaveDraft')));
showError(getUserFacingError(error, tErrors, t('errorSaveDraft')));
throw error;
} finally {
saveInFlightRef.current = false;
@@ -711,7 +712,7 @@ export function TreatmentWorkspace({
const response = await treatmentsApi.listPatientHistory(patientId);
setHistory(response.data);
} catch (error: unknown) {
showError(formatApiErrorMessage(error, t('errorLoadHistory')));
showError(getUserFacingError(error, tErrors, t('errorLoadHistory')));
}
}, [showError, t]);
@@ -864,7 +865,7 @@ export function TreatmentWorkspace({
);
showSuccess(t('successFilesUploaded', { count: uploaded.data.length }));
} catch (error: unknown) {
showError(formatApiErrorMessage(error, t('errorUpload')));
showError(getUserFacingError(error, tErrors, t('errorUpload')));
} finally {
setUploadBusyDetailId(null);
}
@@ -954,7 +955,7 @@ export function TreatmentWorkspace({
const saved = await persistDraft({ force: true });
await persistLabCases(saved, cleaned);
} catch (error: unknown) {
showError(formatApiErrorMessage(error, t('errorSaveLabShipments')));
showError(getUserFacingError(error, tErrors, t('errorSaveLabShipments')));
}
})();
}
@@ -999,7 +1000,7 @@ export function TreatmentWorkspace({
const saved = await persistDraft({ force: true });
await persistLabCases(saved, updatedLabCases);
} catch (error: unknown) {
showError(formatApiErrorMessage(error, t('errorSaveLabShipments')));
showError(getUserFacingError(error, tErrors, t('errorSaveLabShipments')));
}
return;
}
@@ -1016,7 +1017,7 @@ export function TreatmentWorkspace({
const saved = await persistDraft({ force: true });
await persistLabCases(saved, updatedLabCases);
} catch (error: unknown) {
showError(formatApiErrorMessage(error, t('errorSaveLabShipments')));
showError(getUserFacingError(error, tErrors, t('errorSaveLabShipments')));
}
}, [
activeDetailId,
@@ -1104,7 +1105,7 @@ export function TreatmentWorkspace({
});
showSuccess(t('successCaseSent'));
} catch (error: unknown) {
showError(formatApiErrorMessage(error, t('errorSendCase')));
showError(getUserFacingError(error, tErrors, t('errorSendCase')));
} finally {
setSendBusyId(null);
}