1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public import class Foundation.NSError
public import class Foundation.NSErrorDomain
public import var Foundation.NSLocalizedDescriptionKey
public import var Foundation.NSOSStatusErrorDomain
public import protocol Foundation.LocalizedError
import SWBLibc
public enum StubError: Error, LocalizedError, CustomStringConvertible, Serializable, Equatable, Sendable {
case error(String)
public var description: String {
switch self {
case .error(let s): return s
}
}
public var errorDescription: String? { return self.description }
public func serialize<T: Serializer>(to serializer: T) {
switch self {
case .error(let s):
serializer.serialize(s)
}
}
public init(from deserializer: any Deserializer) throws {
let s = try deserializer.deserialize() as String
self = .error(s)
}
}
#if canImport(Darwin)
public struct MacError: Error, CustomStringConvertible, LocalizedError {
public let code: OSStatus
private let error: NSError
public init(_ code: OSStatus) {
self.code = code
self.error = NSError(domain: NSOSStatusErrorDomain, code: Int(code), userInfo: nil)
}
public var description: String {
// <rdar://42459842> No supported API is available for retrieving this information in a nicer way
let prefix = "Error Domain=NSOSStatusErrorDomain Code=\(code) \""
let suffix = "\""
var rawDescription = error.description
guard rawDescription.hasPrefix(prefix) else {
return rawDescription
}
rawDescription = rawDescription.withoutPrefix(prefix)
if rawDescription.hasSuffix(suffix) {
rawDescription = rawDescription.withoutSuffix(suffix)
}
let (first, second) = rawDescription.split(":")
let constant: String
let message: String
if !second.isEmpty {
constant = first
message = String(second.dropFirst())
} else {
constant = ""
message = first
}
if !constant.isEmpty {
return "\(message) (\(constant), \(code))"
} else {
if message == "(null)" || message.isEmpty {
// Provide a friendlier message in these odd cases.
return "The operation couldn’t be completed. (\(code))"
}
else {
return "\(message) (\(code))"
}
}
}
public var errorDescription: String? { return description }
}
#endif
|