-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#434: Show specific error message in FE via Toast pop up.
* If you want to start a study with e.g. an empty lime survey id, a Toast pop up will be shown, that the ID is missing. * Add global Toast component to show erroneous API response. * Add html required attribute if property is required (not only a * symbol next to the label). * Add trim to v-model for String properties, to trim whitespaced on users input. * Add error translations. * Replace alter messages with Toast pop ups.
- Loading branch information
Showing
16 changed files
with
192 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright LBI-DHP and/or licensed to LBI-DHP under one or more | ||
contributor license agreements (LBI-DHP: Ludwig Boltzmann Institute | ||
for Digital Health and Prevention -- A research institute of the | ||
Ludwig Boltzmann Gesellschaft, Oesterreichische Vereinigung zur | ||
Foerderung der wissenschaftlichen Forschung). | ||
Licensed under the Elastic License 2.0. | ||
*/ | ||
import { inject, Ref } from 'vue'; | ||
import { ToastServiceMethods } from 'primevue/toastservice'; | ||
import { ToastMessageOptions } from 'primevue/toast'; | ||
import { useI18n } from 'vue-i18n'; | ||
import { | ||
ValidationReport, | ||
ValidationReportItem, | ||
} from '../generated-sources/openapi'; | ||
|
||
/** | ||
* This composable is responsible for showing errors from Backend to the Client with a Toast popup. | ||
*/ | ||
export function useToastService(): any { | ||
const toast: Ref<ToastServiceMethods> | undefined = | ||
inject<Ref<ToastServiceMethods>>('toast'); | ||
const { t } = useI18n(); | ||
|
||
if (!toast) { | ||
throw new Error('ToastService is not provided'); | ||
} | ||
|
||
const handleToastErrors = (err: any): void => { | ||
processConfigurationException(err); | ||
processOtherExceptions(err); | ||
}; | ||
|
||
function processConfigurationException(report: ValidationReport): void { | ||
const errors: string[] = []; | ||
const warnings: string[] = []; | ||
|
||
report.errors?.forEach((error: ValidationReportItem) => { | ||
const errorMessage = error.message | ||
? t(error.message, { value: error.propertyId }) | ||
: null; | ||
if (errorMessage) { | ||
errors.push(errorMessage); | ||
} | ||
}); | ||
|
||
report.warnings?.forEach((warning: ValidationReportItem) => { | ||
const warningMessage = warning.message | ||
? t(warning.message, { value: warning.propertyId }) | ||
: null; | ||
if (warningMessage) { | ||
warnings.push(warningMessage); | ||
} | ||
}); | ||
|
||
if (errors.length) { | ||
showErrorToast(errors.join('\n')); | ||
} | ||
|
||
if (warnings.length) { | ||
showWarningToast(warnings.join('\n')); | ||
} | ||
} | ||
|
||
function processOtherExceptions(error: any): void { | ||
if (error.message) { | ||
showErrorToast(error.message); | ||
} | ||
} | ||
|
||
const showToast = ( | ||
severity: ToastMessageOptions['severity'], | ||
summary: string, | ||
detail: string, | ||
): void => { | ||
if (toast.value) { | ||
toast.value.add({ severity, summary, detail }); | ||
} else { | ||
console.error('Toast reference is not set'); | ||
} | ||
}; | ||
|
||
const showErrorToast = (detail: string): void => { | ||
if (toast.value) { | ||
const severity: ToastMessageOptions['severity'] = 'error'; | ||
const summary = t('global.error.type.error'); | ||
const life: ToastMessageOptions['life'] = 5000; | ||
|
||
toast.value.add({ severity, summary, detail, life }); | ||
} else { | ||
console.error('Toast reference is not set'); | ||
} | ||
}; | ||
|
||
const showWarningToast = (detail: string): void => { | ||
if (toast.value) { | ||
const severity: ToastMessageOptions['severity'] = 'warn'; | ||
const summary = t('global.error.type.warning'); | ||
const life: ToastMessageOptions['life'] = 5000; | ||
|
||
toast.value.add({ severity, summary, detail, life }); | ||
} else { | ||
console.error('Toast reference is not set'); | ||
} | ||
}; | ||
|
||
return { | ||
handleToastErrors, | ||
showToast, | ||
showErrorToast, | ||
showWarningToast, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.