From b540e0ba730864ffd3bb215a8074f9c831460944 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 14 Dec 2025 22:19:04 +0100 Subject: [PATCH] JSON parsing lines error handling improved in APIs. See #550 --- js/workers/model-worker-google_gemini.js | 12 +++++++++--- js/workers/model-worker-ollama.js | 12 +++++++++--- js/workers/model-worker-openai.js | 12 +++++++++--- js/workers/model-worker-openai_comp.js | 12 +++++++++--- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/js/workers/model-worker-google_gemini.js b/js/workers/model-worker-google_gemini.js index 8ea818eb..cce3cf8b 100644 --- a/js/workers/model-worker-google_gemini.js +++ b/js/workers/model-worker-google_gemini.js @@ -106,9 +106,15 @@ self.onmessage = async function(event) { .filter((line) => line !== "" ) // Remove empty lines // .map((line) => JSON.parse(line)); // Parse the JSON string .map((line) => { - taLog.log("line: " + JSON.stringify(line)); - return JSON.parse(line); - }); + try { + taLog.log("line: " + JSON.stringify(line)); + return JSON.parse(line); + } catch (e) { + taLog.warn("JSON parse warning, skipped line: " + line + " - " + e.message); + return null; + } + }) + .filter((parsed) => parsed !== null); }catch(e){ taLog.error("Error parsing lines: " + e); } diff --git a/js/workers/model-worker-ollama.js b/js/workers/model-worker-ollama.js index c04f0ef6..d099cf2a 100644 --- a/js/workers/model-worker-ollama.js +++ b/js/workers/model-worker-ollama.js @@ -112,9 +112,15 @@ self.onmessage = async function(event) { .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]" // .map((line) => JSON.parse(line)); // Parse the JSON string .map((line) => { - taLog.log("line: " + JSON.stringify(line)); - return JSON.parse(line); - }); + try { + taLog.log("line: " + JSON.stringify(line)); + return JSON.parse(line); + } catch (e) { + taLog.warn("JSON parse warning, skipped line: " + line + " - " + e.message); + return null; + } + }) + .filter((parsed) => parsed !== null); }catch(e){ taLog.error("Error parsing lines: " + e); } diff --git a/js/workers/model-worker-openai.js b/js/workers/model-worker-openai.js index f09d06d4..1cd9055b 100644 --- a/js/workers/model-worker-openai.js +++ b/js/workers/model-worker-openai.js @@ -106,9 +106,15 @@ self.onmessage = async function(event) { .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]" // .map((line) => JSON.parse(line)); // Parse the JSON string .map((line) => { - taLog.log("line: " + JSON.stringify(line)); - return JSON.parse(line); - }); + try { + taLog.log("line: " + JSON.stringify(line)); + return JSON.parse(line); + } catch (e) { + taLog.warn("JSON parse warning, skipped line: " + line + " - " + e.message); + return null; + } + }) + .filter((parsed) => parsed !== null); }catch(e){ taLog.error("Error parsing lines: " + e); } diff --git a/js/workers/model-worker-openai_comp.js b/js/workers/model-worker-openai_comp.js index 9e9ccaeb..333ce148 100644 --- a/js/workers/model-worker-openai_comp.js +++ b/js/workers/model-worker-openai_comp.js @@ -111,9 +111,15 @@ self.onmessage = async function(event) { .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]" // .map((line) => JSON.parse(line)); // Parse the JSON string .map((line) => { - taLog.log("line: " + JSON.stringify(line)); - return JSON.parse(line); - }); + try { + taLog.log("line: " + JSON.stringify(line)); + return JSON.parse(line); + } catch (e) { + taLog.warn("JSON parse warning, skipped line: " + line + " - " + e.message); + return null; + } + }) + .filter((parsed) => parsed !== null); }catch(e){ taLog.error("Error parsing lines: " + e); }