ApiError.swift 869 Bytes
Newer Older
1
2
3
4
5
6
7
8
//
//  ApiError.swift
//  Pods
//
//  Created by Mauro Bender on 25/8/16.
//
//

Mauro Bender's avatar
Mauro Bender committed
9
10
import Alamofire

11
12
13
14
15
public enum ApiError: Error {
	case network( error: Error, response: DataResponse<Any> )
	case invalidURL(url: URLConvertible)
	case jsonSerialization(error: AFError)
	case unknown( error: Error )
16
17
18
}

public extension ApiError {
19
	init( response: DataResponse<Any> ) {
20
21
		let error = response.result.error!
		
22
23
24
25
26
27
28
29
30
31
32
		if let statusCode = response.response?.statusCode , !(200...299).contains( statusCode ) {
			self = .network( error: error, response: response )
		} else if let error = error as? AFError {
			switch error {
			case .responseSerializationFailed(_):
				self = .jsonSerialization( error: error )
			case .invalidURL(let url):
				self = .invalidURL(url: url)
			default:
				self = .unknown(error: error)
			}
33
		} else {
34
			self = .unknown(error: error)
35
36
		}
	}
37
}