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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2020 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 Swift project authors
*/
import TSCBasic
/// Represents an object which can be converted into a diagnostic data.
// FIXME: Kill this
public protocol DiagnosticDataConvertible {
/// Diagnostic data representation of this instance.
var diagnosticData: DiagnosticData { get }
}
/// Protocol for types that can provide a diagnostic location. A common use is
/// for specializations of Swift.Error that can have diagnostic locations.
public protocol DiagnosticLocationProviding {
var diagnosticLocation: DiagnosticLocation? { get }
}
/// DiagnosticData wrapper for Swift errors.
public struct AnyDiagnostic: DiagnosticData {
public let anyError: Swift.Error
public init(_ error: Swift.Error) {
self.anyError = error
}
public var description: String {
return "\(anyError)"
}
}
extension DiagnosticsEngine {
public func emit(
error: String,
location: DiagnosticLocation? = nil
) {
emit(.error(error), location: location)
}
public func emit(
warning: String,
location: DiagnosticLocation? = nil
) {
emit(.warning(warning), location: location)
}
public func emit(
note: String,
location: DiagnosticLocation? = nil
) {
emit(.note(note), location: location)
}
public func emit(
remark: String,
location: DiagnosticLocation? = nil
) {
emit(.remark(remark), location: location)
}
/// Emit a Swift error.
///
/// Errors will be converted into diagnostic data if possible.
/// Otherwise, they will be emitted as AnyDiagnostic.
public func emit(
_ error: Swift.Error,
location: DiagnosticLocation? = nil
) {
let location = location ?? (error as? DiagnosticLocationProviding)?.diagnosticLocation
if let diagnosticData = error as? DiagnosticData {
emit(.error(diagnosticData), location: location)
} else if case let convertible as DiagnosticDataConvertible = error {
emit(convertible, location: location)
} else {
emit(.error(AnyDiagnostic(error)), location: location)
}
}
/// Emit a diagnostic data convertible instance.
public func emit(
_ convertible: DiagnosticDataConvertible,
location: DiagnosticLocation? = nil
) {
emit(.error(convertible.diagnosticData), location: location)
}
@discardableResult
public func with<T>(location: DiagnosticLocation, _ closure: (DiagnosticsEngine) -> T) -> T {
let innerDiagnostics = DiagnosticsEngine(handlers: [self.emit], defaultLocation: location)
return closure(innerDiagnostics)
}
/// Wrap a throwing closure, returning an optional value and
/// emitting any thrown errors.
///
/// - Parameters:
/// - closure: Closure to wrap.
/// - Returns: Returns the return value of the closure wrapped
/// into an optional. If the closure throws, nil is returned.
public func wrap<T>(_ closure: () throws -> T) -> T? {
do {
return try closure()
} catch Diagnostics.fatalError {
return nil
} catch {
emit(error)
return nil
}
}
/// Wrap a throwing closure, returning a success boolean and
/// emitting any thrown errors.
///
/// - Parameters:
/// - closure: Closure to wrap.
/// - Returns: Returns true if the wrapped closure did not throw
/// and false otherwise.
@discardableResult
public func wrap(_ closure: () throws -> Void) -> Bool {
do {
try closure()
return true
} catch Diagnostics.fatalError {
return false
} catch {
emit(error)
return false
}
}
}
extension Optional where Wrapped == DiagnosticsEngine {
public func emit(
error: String,
location: DiagnosticLocation? = nil
) throws {
if case let diagnostics? = self {
diagnostics.emit(.error(error), location: location)
} else {
throw StringError(error)
}
}
public func emit(
_ error: Swift.Error,
location: DiagnosticLocation? = nil
) throws {
if case let diagnostics? = self {
diagnostics.emit(error, location: location)
} else {
throw error
}
}
public func emit(
_ convertible: DiagnosticDataConvertible,
location: DiagnosticLocation? = nil
) throws {
if case let diagnostics? = self {
diagnostics.emit(.error(convertible.diagnosticData), location: location)
} else {
throw StringError(convertible.diagnosticData.description)
}
}
public func emit(
_ message: Diagnostic.Message,
location: DiagnosticLocation? = nil
) throws {
if case let diagnostics? = self {
diagnostics.emit(message, location: location)
} else if message.behavior == .error {
throw StringError(message.text)
}
}
}
/// Namespace for representing diagnostic location of a package.
// deprecated 9/2021
@available(*, deprecated)
public enum PackageLocation {
/// Represents location of a locally available package. This could be root
/// package, edited dependency or checked out dependency.
public struct Local: DiagnosticLocation {
/// The name of the package, if known.
public let name: String?
/// The path to the package.
public let packagePath: AbsolutePath
public init(name: String? = nil, packagePath: AbsolutePath) {
self.name = name
self.packagePath = packagePath
}
public var description: String {
let stream = BufferedOutputByteStream()
if let name = name {
stream.send("'\(name)' ")
}
stream.send(packagePath)
return stream.bytes.description
}
}
/// Represents location a remote package with no checkout on disk.
public struct Remote: DiagnosticLocation {
/// The URL of the package.
public let url: String
/// The source control reference of the package. It could be version, branch, revision etc.
public let reference: String
public init(url: String, reference: String) {
self.url = url
self.reference = reference
}
public var description: String {
return url + " @ " + reference
}
}
}
/// An Swift error enum that can be used as a stub to early exit from a method.
///
/// It is not expected for this enum to contain any payload or information about the
/// error. The actual errors and warnings are supposed to be added using the Diagnostics
/// engine.
public enum Diagnostics: Swift.Error {
case fatalError
}
|