extract throwError helper to reduce repetition

This commit is contained in:
mahula 2025-11-12 12:26:33 +01:00
parent 73bcb71754
commit 6bc5a43219

View File

@ -26,6 +26,11 @@ const stripSlashes = s => s.replace(/^\/+|\/+$/g, '');
const locale = stripSlashes(useRouteLocale().value) || 'de' const locale = stripSlashes(useRouteLocale().value) || 'de'
const lang = usePageLang().value || 'de-DE' const lang = usePageLang().value || 'de-DE'
const throwError = (message) => {
console.error(message)
throw new Error(message)
}
const props = defineProps({ const props = defineProps({
currentValue: { currentValue: {
type: Number, type: Number,
@ -50,96 +55,62 @@ const props = defineProps({
}) })
if (typeof props.currentValue !== 'number') { if (typeof props.currentValue !== 'number') {
const error = `[DonationBar] Prop "currentValue" must be a number, received: ${typeof props.currentValue} (${props.currentValue})` throwError(`[DonationBar] Prop "currentValue" must be a number, received: ${typeof props.currentValue} (${props.currentValue})`)
console.error(error)
throw new Error(error)
} }
if (isNaN(props.currentValue)) { if (isNaN(props.currentValue)) {
const error = `[DonationBar] Prop "currentValue" must be a valid number, received: NaN` throwError(`[DonationBar] Prop "currentValue" must be a valid number, received: NaN`)
console.error(error)
throw new Error(error)
} }
if (!isFinite(props.currentValue)) { if (!isFinite(props.currentValue)) {
const error = `[DonationBar] Prop "currentValue" must be a finite number, received: ${props.currentValue}` throwError(`[DonationBar] Prop "currentValue" must be a finite number, received: ${props.currentValue}`)
console.error(error)
throw new Error(error)
} }
if (props.currentValue < 0) { if (props.currentValue < 0) {
const error = `[DonationBar] Prop "currentValue" must be >= 0, received: ${props.currentValue}` throwError(`[DonationBar] Prop "currentValue" must be >= 0, received: ${props.currentValue}`)
console.error(error)
throw new Error(error)
} }
if (typeof props.target !== 'number') { if (typeof props.target !== 'number') {
const error = `[DonationBar] Prop "target" must be a number, received: ${typeof props.target} (${props.target})` throwError(`[DonationBar] Prop "target" must be a number, received: ${typeof props.target} (${props.target})`)
console.error(error)
throw new Error(error)
} }
if (isNaN(props.target)) { if (isNaN(props.target)) {
const error = `[DonationBar] Prop "target" must be a valid number, received: NaN` throwError(`[DonationBar] Prop "target" must be a valid number, received: NaN`)
console.error(error)
throw new Error(error)
} }
if (!isFinite(props.target)) { if (!isFinite(props.target)) {
const error = `[DonationBar] Prop "target" must be a finite number, received: ${props.target}` throwError(`[DonationBar] Prop "target" must be a finite number, received: ${props.target}`)
console.error(error)
throw new Error(error)
} }
if (props.target <= 0) { if (props.target <= 0) {
const error = `[DonationBar] Prop "target" must be > 0, received: ${props.target}` throwError(`[DonationBar] Prop "target" must be > 0, received: ${props.target}`)
console.error(error)
throw new Error(error)
} }
if (typeof props.startDate !== 'string') { if (typeof props.startDate !== 'string') {
const error = `[DonationBar] Prop "startDate" must be a string, received: ${typeof props.startDate}` throwError(`[DonationBar] Prop "startDate" must be a string, received: ${typeof props.startDate}`)
console.error(error)
throw new Error(error)
} }
const startDateRegex = /^\d{4}-\d{2}-\d{2}$/ const startDateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!startDateRegex.test(props.startDate)) { if (!startDateRegex.test(props.startDate)) {
const error = `[DonationBar] Prop "startDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.startDate}` throwError(`[DonationBar] Prop "startDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.startDate}`)
console.error(error)
throw new Error(error)
} }
if (isNaN(new Date(props.startDate).getTime())) { if (isNaN(new Date(props.startDate).getTime())) {
const error = `[DonationBar] Prop "startDate" has invalid date value: ${props.startDate}` throwError(`[DonationBar] Prop "startDate" has invalid date value: ${props.startDate}`)
console.error(error)
throw new Error(error)
} }
if (typeof props.endDate !== 'string') { if (typeof props.endDate !== 'string') {
const error = `[DonationBar] Prop "endDate" must be a string, received: ${typeof props.endDate}` throwError(`[DonationBar] Prop "endDate" must be a string, received: ${typeof props.endDate}`)
console.error(error)
throw new Error(error)
} }
const endDateRegex = /^\d{4}-\d{2}-\d{2}$/ const endDateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!endDateRegex.test(props.endDate)) { if (!endDateRegex.test(props.endDate)) {
const error = `[DonationBar] Prop "endDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.endDate}` throwError(`[DonationBar] Prop "endDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.endDate}`)
console.error(error)
throw new Error(error)
} }
if (isNaN(new Date(props.endDate).getTime())) { if (isNaN(new Date(props.endDate).getTime())) {
const error = `[DonationBar] Prop "endDate" has invalid date value: ${props.endDate}` throwError(`[DonationBar] Prop "endDate" has invalid date value: ${props.endDate}`)
console.error(error)
throw new Error(error)
} }
if (typeof props.asOfDate !== 'string') { if (typeof props.asOfDate !== 'string') {
const error = `[DonationBar] Prop "asOfDate" must be a string, received: ${typeof props.asOfDate}` throwError(`[DonationBar] Prop "asOfDate" must be a string, received: ${typeof props.asOfDate}`)
console.error(error)
throw new Error(error)
} }
const asOfDateRegex = /^\d{4}-\d{2}-\d{2}$/ const asOfDateRegex = /^\d{4}-\d{2}-\d{2}$/
if (!asOfDateRegex.test(props.asOfDate)) { if (!asOfDateRegex.test(props.asOfDate)) {
const error = `[DonationBar] Prop "asOfDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.asOfDate}` throwError(`[DonationBar] Prop "asOfDate" must be in ISO 8601 format (YYYY-MM-DD), received: ${props.asOfDate}`)
console.error(error)
throw new Error(error)
} }
if (isNaN(new Date(props.asOfDate).getTime())) { if (isNaN(new Date(props.asOfDate).getTime())) {
const error = `[DonationBar] Prop "asOfDate" has invalid date value: ${props.asOfDate}` throwError(`[DonationBar] Prop "asOfDate" has invalid date value: ${props.asOfDate}`)
console.error(error)
throw new Error(error)
} }
const title = computed(() => { const title = computed(() => {