From 770e6bc8e5a320a8a0bbaddb13200f5b6bd41a94 Mon Sep 17 00:00:00 2001 From: LeafYeeXYZ Date: Sun, 12 May 2024 14:12:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Prompt.tsx | 15 +++++++++++++-- src/components/Widgets/PromptGenerator.tsx | 2 +- src/styles/Dialog.css | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/components/Prompt.tsx b/src/components/Prompt.tsx index bfd27e7..c313175 100644 --- a/src/components/Prompt.tsx +++ b/src/components/Prompt.tsx @@ -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 重复, 不添加图片 diff --git a/src/components/Widgets/PromptGenerator.tsx b/src/components/Widgets/PromptGenerator.tsx index 78f0276..82517b6 100644 --- a/src/components/Widgets/PromptGenerator.tsx +++ b/src/components/Widgets/PromptGenerator.tsx @@ -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 diff --git a/src/styles/Dialog.css b/src/styles/Dialog.css index ba638ec..b218261 100644 --- a/src/styles/Dialog.css +++ b/src/styles/Dialog.css @@ -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;