summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/ChainedError.swift
blob: 86977609a1c729fd8dd0864d7d96bfa22af6a6d9 (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
54
55
56
57
58
59
60
61
62
63
64
65
//
//  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 }
}

final class AnyChainedError: ChainedError {
    private let wrappedError: Error

    init(_ error: Error) {
        wrappedError = error
    }

    var errorDescription: String? {
        return wrappedError.localizedDescription
    }
}

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
        }
    }
}