blob: cadad2609c85f4f7d11ae2a97ccf5748efe88d6d (
plain)
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
|
//
// ErrorChain.swift
// MullvadVPN
//
// Created by pronebird on 07/05/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
import Foundation
/// A protocol describing errors that can be chained together
protocol ChainedError: LocalizedError {
/// A source error when available
var source: Error? { get }
}
extension ChainedError {
var source: Error? {
let reflection = Mirror(reflecting: self)
if case .enum = reflection.displayStyle {
for child in reflection.children {
if let associatedError = child.value as? Error {
return associatedError
}
}
}
return nil
}
/// Creates a string representation of the entire error chain.
/// An extra `message` is added at the start of the chain when given
func displayChain(message: String? = nil) -> String {
var s = message.map { "Error: \($0)\nCaused by: \(self.localizedDescription)" }
?? "Error: \(self.localizedDescription)"
for sourceError in makeChainIterator() {
s.append("\nCaused by: \(sourceError.localizedDescription)")
}
return s
}
private func makeChainIterator() -> AnyIterator<Error> {
var current: Error? = self
return AnyIterator { () -> Error? in
current = (current as? ChainedError)?.source
return current
}
}
}
|