فهرست منبع

[eval] throw catchable exception if failed to read/write a file (fixes #8098)

Alexander Kuzmenko 6 سال پیش
والد
کامیت
da81c978c6
2فایلهای تغییر یافته به همراه16 افزوده شده و 2 حذف شده
  1. 8 2
      src/macro/eval/evalStdLib.ml
  2. 8 0
      tests/sys/src/io/TestFile.hx

+ 8 - 2
src/macro/eval/evalStdLib.ml

@@ -965,7 +965,10 @@ module StdFile = struct
 		let perms = 0o666 in
 		let l = Open_creat :: flags in
 		let l = if binary then Open_binary :: l else l in
-		let ch = open_out_gen l perms path in
+		let ch =
+			try open_out_gen l perms path
+			with Sys_error msg -> exc_string msg
+		in
 		encode_instance key_sys_io_FileOutput ~kind:(IOutChannel ch)
 
 	let write_out path content =
@@ -1001,7 +1004,10 @@ module StdFile = struct
 			| VTrue | VNull -> true
 			| _ -> false
 		in
-		let ch = open_in_gen (Open_rdonly :: (if binary then [Open_binary] else [])) 0 path in
+		let ch =
+			try open_in_gen (Open_rdonly :: (if binary then [Open_binary] else [])) 0 path
+			with Sys_error msg -> exc_string msg
+		in
 		encode_instance key_sys_io_FileInput ~kind:(IInChannel(ch,ref false))
 	)
 

+ 8 - 0
tests/sys/src/io/TestFile.hx

@@ -20,4 +20,12 @@ class TestFile extends utest.Test {
 		FileSystem.deleteFile(fileA);
 		FileSystem.deleteFile(fileB);
 	}
+
+	public function testCopyNonExistentSource() {
+		try {
+			File.copy('non-existent-src', 'non-existent-dst');
+		} catch(e:Dynamic) {
+			//see https://github.com/HaxeFoundation/haxe/issues/8098
+		}
+	}
 }