changeset 39:2a3092ca528e

error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 15 May 2025 16:24:04 -0600
parents 8c90e0077cfe
children 38bc1cdf77c8
files src/luan_editor/window.luan
diffstat 1 files changed, 28 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
diff -r 8c90e0077cfe -r 2a3092ca528e src/luan_editor/window.luan
--- a/src/luan_editor/window.luan	Thu May 15 11:03:48 2025 -0600
+++ b/src/luan_editor/window.luan	Thu May 15 16:24:04 2025 -0600
@@ -24,6 +24,8 @@
 local Swing = require "luan:swing/Swing.luan"
 local File_chooser = require "luan:swing/File_chooser.luan"
 local choose_file = File_chooser.awt_choose_file or error()
+local Option_pane = require "luan:swing/Option_pane.luan"
+local show_message_dialog = Option_pane.show_message_dialog or error()
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "editor/window"
 
@@ -59,7 +61,19 @@
 
 local function new_window(file,document)
 	local window = {}
-	window.has_file = file~=nil and file.is_file()
+	if file == nil or not file.exists() then
+		window.has_file = false
+	elseif file.is_file() then
+		window.has_file = true
+	else
+		show_message_dialog(nil,"Not a file")
+		if n_windows == 0 then
+			Luan.exit()
+		else
+			return
+		end
+	end
+	window.has_file = file~=nil and file.exists()
 	local text_area = new_text_area{
 		wrap_style_word = true
 		line_wrap = config.line_wrap
@@ -73,14 +87,14 @@
 		text_area.document = document
 	elseif file ~= nil then
 		local document = documents[title]
-		if document == nil then
-			documents[title] = text_area.document
-		else
+		if document ~= nil then
 			text_area.document = document
-		end
-		if file.is_file() then
-			text_area.text = file.read_text()
-			text_area.document.clear_unedited()
+		else
+			documents[title] = text_area.document
+			if file.exists() then
+				text_area.text = file.read_text()
+				text_area.document.clear_unedited()
+			end
 		end
 	end
 	text_area.set_selection(0)
@@ -161,7 +175,12 @@
 			frame.title = title
 			documents[title] = text_area.document
 		end
-		file.write_text(text_area.text)
+		try
+			file.write_text(text_area.text)
+		catch e
+			show_message_dialog( frame, e.get_message() )
+			return false
+		end
 		text_area.document.set_unedited()
 		return true
 	end