How to declare Error in Swift

Issue #704

We used to declare enum that conforms to Error, but any type like struct or class can conform to Error as well.

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
enum NetworkError: Error {
case failToCreateRequest
case failToParseResponse
case failToReachServe
}

struct DetailError: Error {
let networkError: Error
let createdAt: Date
let tag: String
}

final class TrackError: Error {
let trackId: String
let detailError: DetailError
let trackSession: String

init(trackId: String, detailError: DetailError, trackSession: String) {
self.trackId = trackId
self.detailError = detailError
self.trackSession = trackSession
}
}

let networkError = NetworkError.failToCreateRequest
let detailError = DetailError(networkError: networkError, createdAt: Date(), tag: "0.1")
let trackError = TrackError(trackId: "AB-01", detailError: detailError, trackSession: "101")

Comments