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