Skip to content

Commit afd84b3

Browse files
committed
1.1.0
1 parent 71ded9b commit afd84b3

File tree

13 files changed

+88
-26
lines changed

13 files changed

+88
-26
lines changed

Ghost.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Ghost'
3-
s.version = '1.0.2'
3+
s.version = '1.1.0'
44
s.summary = 'Versatile HTTP networking framework written in Swift.'
55

66
s.homepage = 'https://github.com/Meniny/Ghost'

Ghost/Core/GhostError.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ import Foundation
1111
public enum GhostError: Error {
1212
case ghost(code: Int?, message: String, headers: [AnyHashable : Any]?, object: Any?, underlying: Error?)
1313
case parse(code: Int?, message: String, object: Any?, underlying: Error?)
14+
15+
public var code: Int? {
16+
switch self {
17+
case .ghost(code: let c, message: _, headers: _, object: _, underlying: _):
18+
return c ?? _code
19+
case .parse(code: let c, message: _, object: _, underlying: _):
20+
return c ?? _code
21+
}
22+
}
23+
24+
public var underlying: Error? {
25+
switch self {
26+
case .ghost(code: _, message: _, headers: _, object: _, underlying: let u):
27+
return u
28+
case .parse(code: _, message: _, object: _, underlying: let u):
29+
return u
30+
}
31+
}
32+
33+
public var message: String? {
34+
switch self {
35+
case .ghost(code: _, message: let m, headers: _, object: _, underlying: _):
36+
return m ?? localizedDescription
37+
case .parse(code: _, message: let m, object: _, underlying: _):
38+
return m ?? localizedDescription
39+
}
40+
}
41+
42+
public var headers: [AnyHashable : Any]? {
43+
switch self {
44+
case .ghost(code: _, message: _, headers: let h, object: _, underlying: _):
45+
return h
46+
default: return nil
47+
}
48+
}
1449
}
1550

1651
extension GhostError {

Ghost/Core/GhostResponse.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ extension GhostResponse {
8282
}
8383

8484
private func handle(_ error: Error) -> Error {
85-
switch error as! GhostError {
85+
guard let e = error as? GhostError else {
86+
return error
87+
}
88+
switch e {
8689
case .parse(let transformCode, let message, let object, let underlying):
8790
return GhostError.parse(code: transformCode ?? statusCode, message: message, object: object ?? responseObject, underlying: underlying)
8891
default:
89-
return error
92+
return e
9093
}
9194
}
9295

Ghost/Core/GhostTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ extension GhostTask {
117117
} else if let error = error {
118118
throw error
119119
} else {
120-
throw GhostError.ghost(code: error?._code, message: error?.localizedDescription ?? "", headers: response?.headers, object: response?.responseObject, underlying: error)
120+
throw GhostError.ghost(code: error?.code, message: error?.message ?? "", headers: response?.headers, object: response?.responseObject, underlying: error?.underlying)
121121
}
122122
}
123123
dispatchSemaphore = DispatchSemaphore(value: 0)

Ghost/Hunter/GhostHunter.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public enum GhostHunterDispatch {
1111

1212
public enum GhostHunterBodyType {
1313
case string(String, encoding: String.Encoding, lossy: Bool)
14-
case json(Encodable)
15-
case plist(Encodable)
14+
// case json(Encodable)
15+
// case plist(Encodable)
1616
case jsonObject([String: Any], options: JSONSerialization.WritingOptions)
1717
case plistObject([String: Any], format: PropertyListSerialization.PropertyListFormat, options: PropertyListSerialization.WriteOptions)
1818
case stream(InputStream)
@@ -102,15 +102,15 @@ internal struct Spoil {
102102
case .jsonObject(let json, let options):
103103
try self.requestBuiler.setJSONBody(json, options: options)
104104
break
105-
case .json(let json):
106-
try self.requestBuiler.setJSONObject(json as? Encodable)
107-
break
105+
// case .json(let json):
106+
// try self.requestBuiler.setJSONObject(json as? Encodable)
107+
// break
108108
case .plistObject(let plist, let format, let options):
109109
try self.requestBuiler.setPlistBody(plist, format: format, options: options)
110110
break
111-
case .plist(let plist):
112-
try self.requestBuiler.setPlistObject(plist as? Encodable)
113-
break
111+
// case .plist(let plist):
112+
// try self.requestBuiler.setPlistObject(plist as? Encodable)
113+
// break
114114
case .multipartFormData(let data):
115115
try self.requestBuiler.setMultipartFormData(data)
116116
break

Ghost/URLSession/GhostURLSession+Data.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension GhostURLSession {
2222

2323
open func data(_ request: URLRequest) throws -> GhostTask {
2424
guard let ghostRequest = request.ghostRequest else {
25-
throw ghostError(URLError(.badURL))!
25+
throw ghostError(URLError(.badURL))
2626
}
2727
return data(ghostRequest)
2828
}
@@ -33,7 +33,7 @@ extension GhostURLSession {
3333

3434
open func data(_ urlString: String, cachePolicy: GhostRequest.GhostCachePolicy? = nil, timeoutInterval: TimeInterval? = nil) throws -> GhostTask {
3535
guard let url = URL(string: urlString) else {
36-
throw ghostError(URLError(.badURL))!
36+
throw ghostError(URLError(.badURL))
3737
}
3838
return data(url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
3939
}

Ghost/URLSession/GhostURLSession+Download.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension GhostURLSession {
3434

3535
open func download(_ request: URLRequest) throws -> GhostTask {
3636
guard let ghostRequest = request.ghostRequest else {
37-
throw ghostError(URLError(.badURL))!
37+
throw ghostError(URLError(.badURL))
3838
}
3939
return download(ghostRequest)
4040
}
@@ -45,7 +45,7 @@ extension GhostURLSession {
4545

4646
open func download(_ urlString: String, cachePolicy: GhostRequest.GhostCachePolicy? = nil, timeoutInterval: TimeInterval? = nil) throws -> GhostTask {
4747
guard let url = URL(string: urlString) else {
48-
throw ghostError(URLError(.badURL))!
48+
throw ghostError(URLError(.badURL))
4949
}
5050
return download(url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
5151
}

Ghost/URLSession/GhostURLSession+Upload.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension GhostURLSession {
1717

1818
open func upload(_ streamedRequest: URLRequest) throws -> GhostTask {
1919
guard let ghostRequest = streamedRequest.ghostRequest else {
20-
throw ghostError(URLError(.badURL))!
20+
throw ghostError(URLError(.badURL))
2121
}
2222
return upload(ghostRequest)
2323
}
@@ -28,7 +28,7 @@ extension GhostURLSession {
2828

2929
open func upload(_ streamedURLString: String, cachePolicy: GhostRequest.GhostCachePolicy? = nil, timeoutInterval: TimeInterval? = nil) throws -> GhostTask {
3030
guard let url = URL(string: streamedURLString) else {
31-
throw ghostError(URLError(.badURL))!
31+
throw ghostError(URLError(.badURL))
3232
}
3333
return upload(url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
3434
}
@@ -47,7 +47,7 @@ extension GhostURLSession {
4747

4848
open func upload(_ request: URLRequest, data: Data) throws -> GhostTask {
4949
guard let ghostRequest = request.ghostRequest else {
50-
throw ghostError(URLError(.badURL))!
50+
throw ghostError(URLError(.badURL))
5151
}
5252
return upload(ghostRequest, data: data)
5353
}
@@ -58,7 +58,7 @@ extension GhostURLSession {
5858

5959
open func upload(_ urlString: String, data: Data, cachePolicy: GhostRequest.GhostCachePolicy? = nil, timeoutInterval: TimeInterval? = nil) throws -> GhostTask {
6060
guard let url = URL(string: urlString) else {
61-
throw ghostError(URLError(.badURL))!
61+
throw ghostError(URLError(.badURL))
6262
}
6363
return upload(url, data: data, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
6464
}
@@ -77,7 +77,7 @@ extension GhostURLSession {
7777

7878
open func upload(_ request: URLRequest, fileURL: URL) throws -> GhostTask {
7979
guard let ghostRequest = request.ghostRequest else {
80-
throw ghostError(URLError(.badURL))!
80+
throw ghostError(URLError(.badURL))
8181
}
8282
return upload(ghostRequest, fileURL: fileURL)
8383
}
@@ -88,7 +88,7 @@ extension GhostURLSession {
8888

8989
open func upload(_ urlString: String, fileURL: URL, cachePolicy: GhostRequest.GhostCachePolicy? = nil, timeoutInterval: TimeInterval? = nil) throws -> GhostTask {
9090
guard let url = URL(string: urlString) else {
91-
throw ghostError(URLError(.badURL))!
91+
throw ghostError(URLError(.badURL))
9292
}
9393
return upload(url, fileURL: fileURL, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
9494
}

Ghost/URLSession/GhostURLSession.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ extension GhostURLSession {
149149
return builder.build()
150150
}
151151

152-
func ghostError(_ error: Error?, _ responseObject: Any? = nil, _ response: URLResponse? = nil) -> GhostError? {
152+
func ghostError(_ error: Error?, _ responseObject: Any? = nil, _ response: URLResponse? = nil) -> GhostError {
153153
if let error = error {
154154
return GhostError.ghost(code: error._code, message: error.localizedDescription, headers: (response as? HTTPURLResponse)?.allHeaderFields, object: responseObject, underlying: error)
155155
}
156-
return nil
156+
return GhostError.ghost(code: 0, message: "Unknown error", headers: nil, object: nil, underlying: nil)
157157
}
158158

159159
func process(_ ghostTask: GhostTask?, _ ghostResponse: GhostResponse?, _ ghostError: GhostError?) {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<p align="center">
33
<img src="./Assets/Ghost.jpg" alt="Ghost">
44
<br/><a href="https://cocoapods.org/pods/Ghost">
5-
<img alt="Version" src="https://img.shields.io/badge/version-1.0.2-brightgreen.svg">
5+
<img alt="Version" src="https://img.shields.io/badge/version-1.1.0-brightgreen.svg">
66
<img alt="Author" src="https://img.shields.io/badge/author-Meniny-blue.svg">
77
<img alt="Build Passing" src="https://img.shields.io/badge/build-passing-brightgreen.svg">
88
<img alt="Swift" src="https://img.shields.io/badge/swift-4.0%2B-orange.svg">

0 commit comments

Comments
 (0)