瀏覽代碼

feat: error handling for when ollama is not running

Timothy J. Baek 1 年之前
父節點
當前提交
5262156c24
共有 2 個文件被更改,包括 32 次插入18 次删除
  1. 16 9
      src/routes/(app)/+page.svelte
  2. 16 9
      src/routes/(app)/c/[id]/+page.svelte

+ 16 - 9
src/routes/(app)/+page.svelte

@@ -189,17 +189,12 @@
 				},
 				format: $settings.requestFormat ?? undefined
 			})
+		}).catch((err) => {
+			console.log(err);
+			return null;
 		});
 
-		if (!res.ok) {
-			const error = await res.json();
-			console.log(error);
-			if ('detail' in error) {
-				toast.error(error.detail);
-			} else {
-				toast.error(error.error);
-			}
-		} else {
+		if (res && res.ok) {
 			const reader = res.body
 				.pipeThrough(new TextDecoderStream())
 				.pipeThrough(splitStream('\n'))
@@ -275,6 +270,18 @@
 					history: history
 				});
 			}
+		} else {
+			if (res !== null) {
+				const error = await res.json();
+				console.log(error);
+				if ('detail' in error) {
+					toast.error(error.detail);
+				} else {
+					toast.error(error.error);
+				}
+			} else {
+				toast.error(`Uh-oh! There was an issue connecting to Ollama.`);
+			}
 		}
 
 		stopResponseFlag = false;

+ 16 - 9
src/routes/(app)/c/[id]/+page.svelte

@@ -216,17 +216,12 @@
 				},
 				format: $settings.requestFormat ?? undefined
 			})
+		}).catch((err) => {
+			console.log(err);
+			return null;
 		});
 
-		if (!res.ok) {
-			const error = await res.json();
-			console.log(error);
-			if ('detail' in error) {
-				toast.error(error.detail);
-			} else {
-				toast.error(error.error);
-			}
-		} else {
+		if (res && res.ok) {
 			const reader = res.body
 				.pipeThrough(new TextDecoderStream())
 				.pipeThrough(splitStream('\n'))
@@ -302,6 +297,18 @@
 					history: history
 				});
 			}
+		} else {
+			if (res !== null) {
+				const error = await res.json();
+				console.log(error);
+				if ('detail' in error) {
+					toast.error(error.detail);
+				} else {
+					toast.error(error.error);
+				}
+			} else {
+				toast.error(`Uh-oh! There was an issue connecting to Ollama.`);
+			}
 		}
 
 		stopResponseFlag = false;