Skip to content

Commit

Permalink
优化错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
LeafYeeXYZ committed May 12, 2024
1 parent 29e8821 commit 770e6bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/components/Prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,20 @@ function Prompt({ children, currentImages, setCurrentImages, dialogAction, langM
})
}
// 解析响应体
const contentType = res.headers.get('content-type') ?? ''
// 判断是否为错误信息
if (contentType.startsWith('application/json')) {
const json = await res.json()
throw new ErrorInfo('生成失败', `服务端返回错误信息: ${JSON.stringify(json)}`)
}
if (!contentType.startsWith('image')) {
throw new ErrorInfo('生成失败', `服务端返回未知的响应类型, 状态码: ${res.status} ${res.statusText}`)
}
// 获取图片 Blob
const blob = await res.blob()
// 根据图片大小判断是否为错误信息
if (blob.size < 1024) throw new ErrorInfo('生成失败', '服务端返回空白图片, 可能是服务器错误或提示词不当')
if (blob.size < 1024) {
throw new ErrorInfo('生成失败', '服务端返回空白图片, 可能是服务器错误或提示词不当')
}
// 获取图片 Hash
const hash = await getHash(blob)
// 如果 hash 重复, 不添加图片
Expand Down
2 changes: 1 addition & 1 deletion src/components/Widgets/PromptGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export default function PromptGenerator({ status, dialogAction, promptRef }: Pro
image: Array.from(uint8array),
}),
})
if (!res.ok) throw new Error(`HTTP ${res.status}`)
// 处理返回数据
const data = await res.json()
if (!res.ok) throw new Error(JSON.stringify(data))
const prompt = data.result.description as string
// 设置提示词
promptRef.current!.value = prompt
Expand Down
1 change: 1 addition & 0 deletions src/styles/Dialog.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
width: 100%;
height: calc(100% - 58px);
text-align: left;
word-wrap: break-word;
padding: 0 18px;
margin-bottom: 18px;
font-family: Microsoft YaHei;
Expand Down

0 comments on commit 770e6bc

Please sign in to comment.