-
Notifications
You must be signed in to change notification settings - Fork 50
/
HandyJSONError.swift
54 lines (48 loc) · 1.3 KB
/
HandyJSONError.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
//
// HandyJSONError.swift
// RxNetworks
//
// Created by Condy on 2021/10/6.
// https://github.com/yangKJ/RxNetworks
import Foundation
@frozen public enum HandyJSONError: Swift.Error {
case error(Error)
case mapModel
case mapArray
case mapCode(Int)
}
extension HandyJSONError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .error(err):
return err.localizedDescription
case .mapModel:
return "Failed to map HandyJSON to an model."
case .mapArray:
return "Failed to map HandyJSON to an model array."
case .mapCode(let code):
return "Failed to map HandyJSON with code (\(code))."
}
}
var underlyingError: Swift.Error? {
switch self {
case .error(let err):
return err
case .mapModel:
return nil
case .mapArray:
return nil
case .mapCode:
return nil
}
}
}
// MARK: - Error User Info
extension HandyJSONError: CustomNSError {
public var errorUserInfo: [String: Any] {
var userInfo: [String: Any] = [:]
userInfo[NSLocalizedDescriptionKey] = errorDescription
userInfo[NSUnderlyingErrorKey] = underlyingError
return userInfo
}
}