Skip to content

Commit ce6f986

Browse files
committed
Add extra logging
1 parent 4bb96e3 commit ce6f986

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

Sources/Foundation/FileHandle.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ extension FileHandle {
826826
return _CFOpenFile(fsRep, flags)
827827
})
828828
if fd < 0 {
829-
throw _NSErrorWithErrno(errno, reading: reading, url: url)
829+
print("_openFileDescriptorForURL(\(url), flags: \(flags), reading: \(reading)) Failed: \(fd)")
830+
throw _NSErrorWithErrno(errno, reading: reading, url: url, extraUserInfo: [NSLocalizedFailureReasonErrorKey:"_openFileDescriptorForURL(\(url), flags: \(flags), reading: \(reading)) Failed: \(fd)"])
830831
}
831832
return fd
832833
}

Sources/Foundation/FoundationErrors.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ internal func _NSErrorWithErrno(_ posixErrno : Int32, reading : Bool, path : Str
211211
// https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes
212212
internal let _NSWindowsErrorDomain = "org.swift.Foundation.WindowsError"
213213

214-
internal func _NSErrorWithWindowsError(_ windowsError: DWORD, reading: Bool, paths: [String]? = nil) -> NSError {
214+
internal func _NSErrorWithWindowsError(_ windowsError: DWORD, reading: Bool, paths: [String]? = nil, extraUserInfo: [String : Any] = [:]) -> NSError {
215215
var cocoaError : CocoaError.Code
216216
switch windowsError {
217217
case DWORD(ERROR_LOCK_VIOLATION): cocoaError = .fileLocking
@@ -269,9 +269,10 @@ internal func _NSErrorWithWindowsError(_ windowsError: DWORD, reading: Bool, pat
269269
}
270270
}
271271
}
272+
273+
var info = extraUserInfo
274+
info[NSUnderlyingErrorKey] = NSError(domain: _NSWindowsErrorDomain, code: Int(windowsError))
272275

273-
return NSError(domain: NSCocoaErrorDomain, code: cocoaError.rawValue, userInfo: [
274-
NSUnderlyingErrorKey: NSError(domain: _NSWindowsErrorDomain, code: Int(windowsError))
275-
])
276+
return NSError(domain: NSCocoaErrorDomain, code: cocoaError.rawValue, userInfo: info)
276277
}
277278
#endif

Sources/Foundation/Process.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -517,18 +517,18 @@ open class Process: NSObject {
517517
var modifiedPipes: [(handle: HANDLE, prevValue: DWORD)] = []
518518
defer { modifiedPipes.forEach { SetHandleInformation($0.handle, DWORD(HANDLE_FLAG_INHERIT), $0.prevValue) } }
519519

520-
func deferReset(handle: HANDLE) throws {
520+
func deferReset(handle: HANDLE, description: String) throws {
521521
var handleInfo: DWORD = 0
522522
guard GetHandleInformation(handle, &handleInfo) else {
523-
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
523+
throw _NSErrorWithWindowsError(GetLastError(), reading: false, extraUserInfo: [NSLocalizedFailureReasonErrorKey:"deferReset(\(handle), \(description)): GetHandleInformation failed"])
524524
}
525525
modifiedPipes.append((handle: handle, prevValue: handleInfo & DWORD(HANDLE_FLAG_INHERIT)))
526526
}
527527

528528
switch standardInput {
529529
case let pipe as Pipe:
530530
siStartupInfo.hStdInput = pipe.fileHandleForReading._handle
531-
try deferReset(handle: pipe.fileHandleForWriting._handle)
531+
try deferReset(handle: pipe.fileHandleForWriting._handle, description: "standardInput as Pipe .fileHandleForReading")
532532
SetHandleInformation(pipe.fileHandleForWriting._handle, DWORD(HANDLE_FLAG_INHERIT), 0)
533533

534534
// nil or NullDevice maps to NUL
@@ -538,15 +538,15 @@ open class Process: NSObject {
538538

539539
case let handle as FileHandle:
540540
siStartupInfo.hStdInput = handle._handle
541-
try deferReset(handle: handle._handle)
541+
try deferReset(handle: handle._handle, description: "standardInput as FileHandle")
542542
SetHandleInformation(handle._handle, DWORD(HANDLE_FLAG_INHERIT), 1)
543543
default: break
544544
}
545545

546546
switch standardOutput {
547547
case let pipe as Pipe:
548548
siStartupInfo.hStdOutput = pipe.fileHandleForWriting._handle
549-
try deferReset(handle: pipe.fileHandleForReading._handle)
549+
try deferReset(handle: pipe.fileHandleForReading._handle, description: "standardOutput as Pipe .fileHandleForReading")
550550
SetHandleInformation(pipe.fileHandleForReading._handle, DWORD(HANDLE_FLAG_INHERIT), 0)
551551

552552
// nil or NullDevice maps to NUL
@@ -556,15 +556,15 @@ open class Process: NSObject {
556556

557557
case let handle as FileHandle:
558558
siStartupInfo.hStdOutput = handle._handle
559-
try deferReset(handle: handle._handle)
559+
try deferReset(handle: handle._handle, description: "standardOutput as FileHandle")
560560
SetHandleInformation(handle._handle, DWORD(HANDLE_FLAG_INHERIT), 1)
561561
default: break
562562
}
563563

564564
switch standardError {
565565
case let pipe as Pipe:
566566
siStartupInfo.hStdError = pipe.fileHandleForWriting._handle
567-
try deferReset(handle: pipe.fileHandleForReading._handle)
567+
try deferReset(handle: pipe.fileHandleForReading._handle, description: "standardError as Pipe .fileHandleForReading")
568568
SetHandleInformation(pipe.fileHandleForReading._handle, DWORD(HANDLE_FLAG_INHERIT), 0)
569569

570570
// nil or NullDevice maps to NUL
@@ -574,7 +574,7 @@ open class Process: NSObject {
574574

575575
case let handle as FileHandle:
576576
siStartupInfo.hStdError = handle._handle
577-
try deferReset(handle: handle._handle)
577+
try deferReset(handle: handle._handle, description: "standardError as FileHandle")
578578
SetHandleInformation(handle._handle, DWORD(HANDLE_FLAG_INHERIT), 1)
579579
default: break
580580
}
@@ -674,9 +674,9 @@ open class Process: NSObject {
674674
// ENOENT, we intercept the error to match the POSIX
675675
// behaviour
676676
if error == ERROR_DIRECTORY {
677-
throw _NSErrorWithWindowsError(DWORD(ERROR_FILE_NOT_FOUND), reading: true)
677+
throw _NSErrorWithWindowsError(DWORD(ERROR_FILE_NOT_FOUND), reading: true, extraUserInfo: [NSLocalizedFailureReasonErrorKey:"CreateProcessW(nil, \(quoteWindowsCommandLine(command)), nil, nil, true, CREATE_UNICODE_ENVIRONMENT, \(szEnvironment), \(workingDirectory), <ptr>, <ptr>) failed with \(error)"])
678678
}
679-
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
679+
throw _NSErrorWithWindowsError(GetLastError(), reading: true, extraUserInfo: [NSLocalizedFailureReasonErrorKey:"CreateProcessW(nil, \(quoteWindowsCommandLine(command)), nil, nil, true, CREATE_UNICODE_ENVIRONMENT, \(szEnvironment), \(workingDirectory), <ptr>, <ptr>) failed with \(error)"])
680680
}
681681
}
682682
}

0 commit comments

Comments
 (0)