Skip to content

Commit

Permalink
🐛 fix: better error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 15, 2024
1 parent 73cda65 commit e3d0c26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/features/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { Flexbox } from 'react-layout-kit';

import { useStore } from '@/store';

const getErrorContent = (errorType: string) => {
switch (errorType) {
const getErrorContent = (errorType: string | { type: string }) => {
if (typeof errorType === 'string') return errorType;

switch (errorType.type) {
case 'NO_BASE_URL': {
return 'MIDJOURNEY API 代理地址为空,请填写后重试';
}
Expand All @@ -36,7 +38,12 @@ const Settings = memo(() => {
>
<Flexbox gap={24}>
{requestError && (
<Alert closable message={getErrorContent(requestError.type)} type={'error'} />
<Alert
closable
description={getErrorContent(requestError.body)}
message={`请求失败,错误码 ${requestError.status}`}
type={'error'}
/>
)}
<Flexbox gap={12}>
<div>MIDJOURNEY API 代理地址</div>
Expand Down
13 changes: 11 additions & 2 deletions src/services/Midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ class MidjourneyService {
});
if (!res.ok) {
const body = await res.text();
throw new Error(body);
throw new Error(JSON.stringify({ body, message: res.statusText, status: res.status }));
}

return res.json();
} catch (error) {
// show Error
const requestError = JSON.parse((error as any).message);

let body = requestError.body;
try {
body = JSON.parse(requestError.body);
} catch {
/* empty */
}

useStore.setState({
isSettingsModalOpen: true,
requestError: JSON.parse((error as any).message),
requestError: { ...requestError, body },
});

return {};
Expand Down
2 changes: 1 addition & 1 deletion src/store/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface AppState {
inLobeChat?: boolean;
isSettingsModalOpen: boolean;
prompts: string;
requestError?: { type: string };
requestError?: { body: string | { type: string }; message: string; status: number };
runningTaskIds: string[];
settings: AppSettings;
tasks: MidjourneyTask[];
Expand Down

0 comments on commit e3d0c26

Please sign in to comment.