/// <summary> /// Creates (but does not throw) an exception for this result. The result must not be successful. /// </summary> public static Exception CreateExceptionForError(this OpenFileResult result) { Contract.Requires(result.Status != OpenFileStatus.Success); return(result.Status == OpenFileStatus.UnknownError ? new NativeWin32Exception(result.NativeErrorCode, "Opening a file handle failed") : new NativeWin32Exception(result.NativeErrorCode, I($"Opening a file handle failed: {result.Status:G}"))); }
/// <summary> /// Creates a <see cref="Failure"/> representing this result. The result must not be successful. /// </summary> public static Failure CreateFailureForError(this OpenFileResult result) { Contract.Requires(result.Status != OpenFileStatus.Success); return (new NativeFailure(result.NativeErrorCode).Annotate( result.Status == OpenFileStatus.UnknownError ? "Opening a file handle failed" : I($"Opening a file handle failed: {result.Status:G}"))); }
/// <summary> /// Returns a string representing information about the <see cref="OpenFileResult"/> error. /// </summary> private static string GetErrorOrFailureMessage(OpenFileResult result) { var message = result.Status == OpenFileStatus.UnknownError ? "Opening a file handle failed" : I($"Opening a file handle failed: {result.Status:G}"); if (result.ImpliesOtherProcessBlockingHandle && result.Path != null) { message += Environment.NewLine; message += FileUtilities.TryFindOpenHandlesToFile(result.Path, out var info) ? info : "Attempt to find processes with open handles to the file failed."; } return(message); }
/// <see cref="IFileSystem.TryPosixDelete(string, out OpenFileResult)"/> public static unsafe bool TryPosixDelete(string pathToDelete, out OpenFileResult openFileResult) { return(s_fileSystem.TryPosixDelete(pathToDelete, out openFileResult)); }
/// <summary> /// Creates a <see cref="Failure"/> representing this result. The result must not be successful. /// </summary> public static Failure CreateFailureForError(this OpenFileResult result) { Contract.Requires(result.Status != OpenFileStatus.Success); return(new NativeFailure(result.NativeErrorCode).Annotate(GetErrorOrFailureMessage(result))); }
/// <summary> /// Creates (but does not throw) an exception for this result. The result must not be successful. /// </summary> public static Exception CreateExceptionForError(this OpenFileResult result) { Contract.Requires(result.Status != OpenFileStatus.Success); return(new NativeWin32Exception(result.NativeErrorCode, GetErrorOrFailureMessage(result))); }
/// <summary> /// Throws an exception for a failed open. /// </summary> public static Exception ThrowForError(this OpenFileResult result) { Contract.Requires(result.Status != OpenFileStatus.Success); throw result.Status == OpenFileStatus.UnknownError ? result.ThrowForUnknownError() : result.ThrowForKnownError(); }
/// <summary> /// Throws an exception if the native error code could not be canonicalized (a fairly exceptional circumstance). /// </summary> /// <remarks> /// This is a good <c>default:</c> case when switching on every possible <see cref="OpenFileStatus"/> /// </remarks> public static Exception ThrowForUnknownError(this OpenFileResult result) { Contract.Requires(result.Status == OpenFileStatus.UnknownError); throw result.CreateExceptionForError(); }