sendfilecontent: Fix indentation and show error if file cannot be opened

Sun, 24 Aug 2014 20:34:04 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 24 Aug 2014 20:34:04 +0100
changeset 103
45843df81db0
parent 102
f1166645a56c
child 122
1dfd28db10bd

sendfilecontent: Fix indentation and show error if file cannot be opened

clix/sendfilecontent.lua file | annotate | diff | comparison | revisions
--- a/clix/sendfilecontent.lua	Wed Aug 20 12:05:46 2014 +0200
+++ b/clix/sendfilecontent.lua	Sun Aug 24 20:34:04 2014 +0100
@@ -3,6 +3,7 @@
 		print("Send file content");
 		return;
 	end
+	
 	local function on_connect(conn)
 		local function send_message(text)
 			conn:send(verse.message({ to = opts.to,
@@ -10,16 +11,20 @@
 				:body(text));
 		end
 		if opts.file then
-            local f = io.open(opts.file, "rb")
-            local content = f:read("*all")
-            f:close()
-            if content:len() < 2000 then
-                send_message(content);
-            else
-                conn:error("File size too large. Cannot send file"); 
-            end
-            conn:close();
+			local f, err = io.open(opts.file, "rb")
+			if not f then
+				conn:error("Unable to open file: %s", err);
+			else
+				local content = f:read("*all")
+				f:close()
+				if content:len() < 2000 then
+					send_message(content);
+				else
+					conn:error("File size too large. Cannot send file");
+				end
+			end
 		end
+		conn:close();
 	end
 	clix_connect(opts, on_connect);
 end

mercurial