Browse Source

Throw correct exceptions for IO errors

NextTurn 6 years ago
parent
commit
b86a028392
1 changed files with 10 additions and 2 deletions
  1. 10 2
      src/Core/WinSWCore/Util/FileHelper.cs

+ 10 - 2
src/Core/WinSWCore/Util/FileHelper.cs

@@ -1,5 +1,5 @@
 #if !NETCOREAPP
-using System.ComponentModel;
+using System;
 #endif
 using System.IO;
 #if !NETCOREAPP
@@ -20,12 +20,20 @@ namespace winsw.Util
 
             if (!NativeMethods.MoveFileEx(sourceFilePath, destFilePath, NativeMethods.MOVEFILE_REPLACE_EXISTING | NativeMethods.MOVEFILE_COPY_ALLOWED))
             {
-                throw new Win32Exception();
+                throw GetExceptionForLastWin32Error(sourceFilePath);
             }
 #endif
         }
 #if !NETCOREAPP
 
+        private static Exception GetExceptionForLastWin32Error(string path) => Marshal.GetLastWin32Error() switch
+        {
+            2 => new FileNotFoundException(null, path), // ERROR_FILE_NOT_FOUND
+            3 => new DirectoryNotFoundException(), // ERROR_PATH_NOT_FOUND
+            5 => new UnauthorizedAccessException(), // ERROR_ACCESS_DENIED
+            _ => new IOException()
+        };
+
         private static class NativeMethods
         {
             internal const uint MOVEFILE_REPLACE_EXISTING = 0x01;