-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubAPIError.swift
More file actions
60 lines (48 loc) · 1.9 KB
/
GitHubAPIError.swift
File metadata and controls
60 lines (48 loc) · 1.9 KB
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
//
// GitHubAPIError.swift
// GitHubSearchRepository
//
// Created by 岡田直道 on 2017/07/12.
// Copyright © 2017年 Naomichi Okada. All rights reserved.
//
import Foundation
struct GitHubAPIError : JSONDecodable, Error {
struct FieldError : JSONDecodable {
let resource: String
let field: String
let code: String
init(json: Any) throws {
guard let dictionary = json as? [String : Any] else {
throw JSONDecodeError.invalidFormat(json: json)
}
guard let resource = dictionary["resource"] as? String else {
throw JSONDecodeError.missingValue(key: "resource", actualValue: dictionary["resource"])
}
guard let field = dictionary["field"] as? String else {
throw JSONDecodeError.missingValue(key: "field", actualValue: dictionary["field"])
}
guard let code = dictionary["code"] as? String else {
throw JSONDecodeError.missingValue(key: "code", actualValue: dictionary["code"])
}
self.resource = resource
self.field = field
self.code = code
}
}
let message: String
let fieldErrors: [FieldError]
init(json : Any) throws {
guard let dictionary = json as? [String : Any] else {
throw JSONDecodeError.invalidFormat(json: json)
}
guard let message = dictionary["message"] as? String else {
throw JSONDecodeError.missingValue(key: "message", actualValue: dictionary["message"])
}
let fieldErrorObjects = dictionary["errors"] as? [Any] ?? []
let fieldErrors = try fieldErrorObjects.map {
return try FieldError(json: $0)
}
self.message = message
self.fieldErrors = fieldErrors
}
}