fix: improve error handling and default values in fetchPlan function (#17320)

This commit is contained in:
akou 2025-04-02 21:44:09 +08:00 committed by GitHub
parent 0bf816f2e8
commit 5cffcd6336
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -111,21 +111,38 @@ export const ProviderContextProvider = ({
const { data: isEducationAccount } = useEducationStatus(!enableEducationPlan) const { data: isEducationAccount } = useEducationStatus(!enableEducationPlan)
const fetchPlan = async () => { const fetchPlan = async () => {
try {
const data = await fetchCurrentPlanInfo() const data = await fetchCurrentPlanInfo()
const enabled = data.billing.enabled if (!data) {
setEnableBilling(enabled) console.error('Failed to fetch plan info: data is undefined')
setEnableEducationPlan(data.education.enabled) return
setIsEducationWorkspace(data.education.activated) }
setEnableReplaceWebAppLogo(data.can_replace_logo)
if (enabled) { // set default value to avoid undefined error
setEnableBilling(data.billing?.enabled ?? false)
setEnableEducationPlan(data.education?.enabled ?? false)
setIsEducationWorkspace(data.education?.activated ?? false)
setEnableReplaceWebAppLogo(data.can_replace_logo ?? false)
if (data.billing?.enabled) {
setPlan(parseCurrentPlan(data) as any) setPlan(parseCurrentPlan(data) as any)
setIsFetchedPlan(true) setIsFetchedPlan(true)
} }
if (data.model_load_balancing_enabled) if (data.model_load_balancing_enabled)
setModelLoadBalancingEnabled(true) setModelLoadBalancingEnabled(true)
if (data.dataset_operator_enabled) if (data.dataset_operator_enabled)
setDatasetOperatorEnabled(true) setDatasetOperatorEnabled(true)
} }
catch (error) {
console.error('Failed to fetch plan info:', error)
// set default value to avoid undefined error
setEnableBilling(false)
setEnableEducationPlan(false)
setIsEducationWorkspace(false)
setEnableReplaceWebAppLogo(false)
}
}
useEffect(() => { useEffect(() => {
fetchPlan() fetchPlan()
}, []) }, [])