Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Improve frontend error handling for non-JSON responses

Murtaza Nasira796171ddbed
diff --git a/static/js/modules/composables/upload.js b/static/js/modules/composables/upload.js
index f56e0d1b..475ef0aa 100644
--- a/static/js/modules/composables/upload.js
+++ b/static/js/modules/composables/upload.js
@@ -240,7 +240,18 @@ export function useUpload(state, utils) {
processingProgress.value = 10;
const response = await fetch('/upload', { method: 'POST', body: formData });
- const data = await response.json();
+
+ // Safely parse JSON response, handling HTML error pages
+ let data;
+ const contentType = response.headers.get('content-type') || '';
+ if (!contentType.includes('application/json')) {
+ const text = await response.text();
+ const titleMatch = text.match(/<title>([^<]+)<\/title>/i);
+ const h1Match = text.match(/<h1>([^<]+)<\/h1>/i);
+ throw new Error(titleMatch?.[1] || h1Match?.[1] ||
+ `Server error (${response.status}): Response was not JSON`);
+ }
+ data = await response.json();
if (!response.ok) {
let errorMsg = data.error || `Upload failed with status ${response.status}`;
@@ -357,6 +368,15 @@ export function useUpload(state, utils) {
const response = await fetch(`/recording/${recordingId}/status`);
if (!response.ok) throw new Error(`Status check failed with status ${response.status}`);
+ // Safely parse JSON response, handling HTML error pages
+ const statusContentType = response.headers.get('content-type') || '';
+ if (!statusContentType.includes('application/json')) {
+ const text = await response.text();
+ const titleMatch = text.match(/<title>([^<]+)<\/title>/i);
+ const h1Match = text.match(/<h1>([^<]+)<\/h1>/i);
+ throw new Error(titleMatch?.[1] || h1Match?.[1] ||
+ `Server error (${response.status}): Status response was not JSON`);
+ }
const statusData = await response.json();
const galleryIndex = recordings.value.findIndex(r => r.id === recordingId);
diff --git a/static/js/utils/apiClient.js b/static/js/utils/apiClient.js
index 39b5783f..56b09c1f 100644
--- a/static/js/utils/apiClient.js
+++ b/static/js/utils/apiClient.js
@@ -11,6 +11,26 @@ class APIError extends Error {
}
}
+/**
+ * Safely parse JSON response, handling HTML error pages gracefully
+ */
+async function safeJsonParse(response) {
+ const contentType = response.headers.get('content-type') || '';
+
+ // If response is not JSON, extract useful error from HTML
+ if (!contentType.includes('application/json')) {
+ const text = await response.text();
+ // Try to extract error message from HTML title or h1
+ const titleMatch = text.match(/<title>([^<]+)<\/title>/i);
+ const h1Match = text.match(/<h1>([^<]+)<\/h1>/i);
+ const errorMsg = titleMatch?.[1] || h1Match?.[1] ||
+ `Server returned non-JSON response (status ${response.status})`;
+ throw new APIError(errorMsg, response.status, { htmlResponse: true });
+ }
+
+ return response.json();
+}
+
export async function apiRequest(url, options = {}) {
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');
@@ -32,7 +52,7 @@ export async function apiRequest(url, options = {}) {
try {
const response = await fetch(url, mergedOptions);
- const data = await response.json();
+ const data = await safeJsonParse(response);
if (!response.ok) {
throw new APIError(