summaryrefslogtreecommitdiff
path: root/src/background
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-09-01 20:52:08 +0900
committertanaka takashi <isazaka@gmail.com>2026-09-01 20:52:08 +0900
commit305c7479a589e7877074588b6467321f0a49cbdf (patch)
treec44a4fb21644fc3d6dc7acf205df527660baee7c /src/background
parentbb06e0e58dcb90eceede6f0b05743fdccc8f51af (diff)
fix: diagnosable translation errors and icon hidden after scroll
Translation error ("設定値が不正です") diagnosis - The INVALID_SETTINGS code collapsed two unrelated failures into one opaque message: (a) no model configured in the background settings cache, and (b) HTTP 400/403 from Ollama with the actual reason in the response body. Verified against a live Ollama server: the configured cloud models (glm-5.3:cloud etc.) stream fine from a plain page, so the generic message gave no clue which path fired. - ollama-client.js: read the Ollama error body on non-OK responses and append its message ("model is required" etc.) to the thrown error - message-router.js: no-model-configured now has its own explicit message ("モデルが設定されていません...") and concrete detail reasons are appended to the base message in the popover, e.g. 設定値が不正です。... (Ollama /api/chat failed: 400 - ...) Action icon disappearing - The scroll handler hid the action button whenever the popover was hidden, even with a live selection, leaving no way to translate until the user re-selected. It now repositions the icon while a selection exists and only hides it when the selection is gone (Design 5.4). Results: 73 passed, 0 failed. dist rebuilt.
Diffstat (limited to 'src/background')
-rw-r--r--src/background/message-router.js17
-rw-r--r--src/background/ollama-client.js26
2 files changed, 36 insertions, 7 deletions
diff --git a/src/background/message-router.js b/src/background/message-router.js
index c93fc9d..9428312 100644
--- a/src/background/message-router.js
+++ b/src/background/message-router.js
@@ -31,13 +31,16 @@ const ERROR_MESSAGES = {
[ERROR_CODES.TEXT_EMPTY]: '選択文字列が空です。',
[ERROR_CODES.TEXT_TOO_LONG]: `選択文字列が長すぎます(上限 ${MAX_SELECTION_LENGTH} 文字)。`,
[ERROR_CODES.OLLAMA_UNAVAILABLE]: 'Ollamaに接続できません。Ollamaが起動しているか確認してください。',
- [ERROR_CODES.MODEL_NOT_FOUND]: '指定されたモデルが見つかりません。',
+ [ERROR_CODES.MODEL_NOT_FOUND]: '指定されたモデルが見つかりません。設定画面でモデルを選び直してください。',
[ERROR_CODES.INVALID_SETTINGS]: '設定値が不正です。設定画面を確認してください。',
[ERROR_CODES.REQUEST_ABORTED]: '翻訳を中断しました。',
[ERROR_CODES.STREAM_ERROR]: '翻訳中にエラーが発生しました。',
[ERROR_CODES.UNKNOWN_ERROR]: '不明なエラーが発生しました。'
};
+/** Message shown when no model is configured at all. */
+const NO_MODEL_MESSAGE = 'モデルが設定されていません。ツールバーアイコンまたは設定画面でモデルを選択してください。';
+
/**
* Handle one content-script port connection.
* @param {object} port runtime.Port
@@ -84,7 +87,7 @@ async function runTranslation(port, requestId, text) {
return;
}
if (!settings.model) {
- postError(port, requestId, ERROR_CODES.INVALID_SETTINGS, 'No model configured');
+ postError(port, requestId, ERROR_CODES.INVALID_SETTINGS, NO_MODEL_MESSAGE);
return;
}
@@ -128,11 +131,15 @@ async function runTranslation(port, requestId, text) {
* @param {object} port
* @param {string} requestId
* @param {string} code
- * @param {string} message
+ * @param {string} detail optional concrete reason shown to the user
*/
-function postError(port, requestId, code, message) {
+function postError(port, requestId, code, detail) {
+ const base = ERROR_MESSAGES[code] || ERROR_MESSAGES[ERROR_CODES.UNKNOWN_ERROR];
+ // Append the concrete server/detail reason when provided so the popover
+ // shows WHY it failed (e.g. the Ollama error body for HTTP 400).
+ const message = detail && detail !== base ? `${base}\n(${detail})` : base;
try {
- port.postMessage({ type: BG_TO_CONTENT.ERROR, requestId, code, message: ERROR_MESSAGES[code] || ERROR_MESSAGES[ERROR_CODES.UNKNOWN_ERROR] });
+ port.postMessage({ type: BG_TO_CONTENT.ERROR, requestId, code, message });
} catch {
// port may be disconnected
}
diff --git a/src/background/ollama-client.js b/src/background/ollama-client.js
index 0286230..e53d997 100644
--- a/src/background/ollama-client.js
+++ b/src/background/ollama-client.js
@@ -16,6 +16,24 @@ import { isValidOllamaHost } from '../shared/settings-schema.js';
import { ERROR_CODES } from '../shared/constants.js';
/**
+ * Extract a short error message from an Ollama JSON error body.
+ * @param {string} raw
+ * @returns {string} message or '' when not parseable
+ */
+function extractOllamaError(raw) {
+ if (!raw) return '';
+ try {
+ const parsed = JSON.parse(raw);
+ if (parsed && typeof parsed.error === 'string') {
+ return parsed.error.slice(0, 200);
+ }
+ } catch {
+ // not JSON
+ }
+ return '';
+}
+
+/**
* Translate a fetch/HTTP failure into a stable error code.
* @param {Response|undefined} response
* @returns {string} error code
@@ -73,7 +91,9 @@ export async function listModels(host, signal) {
throw toExtensionError(error);
}
if (!response.ok) {
- const e = new Error(`Ollama /api/tags failed: ${response.status}`);
+ const detail = await response.text().catch(() => '');
+ const serverMessage = extractOllamaError(detail);
+ const e = new Error(`Ollama /api/tags failed: ${response.status}${serverMessage ? ` - ${serverMessage}` : ''}`);
e.code = response.status === 404 ? ERROR_CODES.MODEL_NOT_FOUND : ERROR_CODES.STREAM_ERROR;
throw e;
}
@@ -119,7 +139,9 @@ export async function chatStream({ host, model, messages, think, options, onChun
}
if (!response.ok) {
- const e = new Error(`Ollama /api/chat failed: ${response.status}`);
+ const detail = await response.text().catch(() => '');
+ const serverMessage = extractOllamaError(detail);
+ const e = new Error(`Ollama /api/chat failed: ${response.status}${serverMessage ? ` - ${serverMessage}` : ''}`);
e.code =
response.status === 404
? ERROR_CODES.MODEL_NOT_FOUND