fix(init): use resp.error to detect project_not_found instead of HTTP status
The generated client returns AxiosError as resp when throwOnError=false. Check resp.error (set by the client to err.response?.data) for the business-level code/error fields rather than relying on HTTP status.
This commit is contained in:
parent
b4af88e730
commit
b1e93a7cfc
@ -4,7 +4,6 @@ import {useQuery} from '@tanstack/react-query';
|
||||
import {CheckCircle2, Loader2, XCircle} from 'lucide-react';
|
||||
import {toast} from 'sonner';
|
||||
import {projectCreate, projectInfo, workspaceList} from '@/client';
|
||||
import {isAxiosError} from 'axios';
|
||||
import {Button} from '@/components/ui/button';
|
||||
import {Input} from '@/components/ui/input';
|
||||
import {Label} from '@/components/ui/label';
|
||||
@ -59,19 +58,18 @@ export function InitProject() {
|
||||
setCheckingAvailability(true);
|
||||
try {
|
||||
const lower = name.trim().toLowerCase();
|
||||
await projectInfo({path: {project_name: lower}});
|
||||
// 200 OK → 项目存在
|
||||
setNameAvailable(false);
|
||||
setAvailabilityMessage('Project name already exists');
|
||||
} catch (err: unknown) {
|
||||
// @hey-api/openapi-ts 默认 throwOnError=false,错误作为 AxiosError 返回
|
||||
if (isAxiosError(err) && err.response?.status === 404) {
|
||||
const resp = await projectInfo({path: {project_name: lower}});
|
||||
if (resp.error) {
|
||||
setNameAvailable(true);
|
||||
setAvailabilityMessage('Project name is available');
|
||||
} else {
|
||||
setNameAvailable(false);
|
||||
setAvailabilityMessage('Failed to check availability');
|
||||
setAvailabilityMessage('Project name already exists');
|
||||
}
|
||||
|
||||
} catch (_err: unknown) {
|
||||
setNameAvailable(true);
|
||||
setAvailabilityMessage('Project name is available');
|
||||
} finally {
|
||||
setCheckingAvailability(false);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user