-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggableError.swift
55 lines (37 loc) · 1.35 KB
/
LoggableError.swift
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
//
// LoggableError.swift
// SimpleLogging
//
// Created by Ben Leggiero on 2020-08-16.
// Copyright © 2020 Ben Leggiero BH-1-PS
//
import Foundation
extension NSError: Loggable {}
/// A special kind of error designed to be logged.
///
/// While all errors can be logged, types conforming to this can be inspected to get the best possible text to be
/// logged, and can customize exactly what is logged if desired.
public protocol LoggableError: Loggable, LocalizedError, CustomStringConvertible {
/// The best text to send to a log which describes this error
var bestDescriptionForLogLine: String { get }
/// A textual representation of this error.
///
/// See also: `CustomStringConvertible`'s `description` field
var description: String { get }
}
public extension LoggableError {
@inline(__always)
var logStringValue: String { bestDescriptionForLogLine }
var bestDescriptionForLogLine: String {
var descriptionSoFar = ""
if let errorDescription = errorDescription {
descriptionSoFar += "\(errorDescription)"
}
if let recoverySuggestion = self.recoverySuggestion {
descriptionSoFar += "\t-\t\(recoverySuggestion)"
}
return descriptionSoFar.isEmpty
? description
: descriptionSoFar
}
}