Skip to content

Commit f700e05

Browse files
Fire completion block failures instead of returning silently (#33)
* Remove unnecessary print statements * Fire completion block failures instead of returning silently * Add missing return statement * Add explicit early returns
1 parent 0674442 commit f700e05

File tree

1 file changed

+185
-48
lines changed

1 file changed

+185
-48
lines changed

Sources/WriteFreely/WFClient.swift

Lines changed: 185 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,19 @@ public class WFClient {
5757
alias: String? = nil,
5858
completion: @escaping (Result<WFCollection, Error>) -> Void
5959
) {
60-
if token == nil && user == nil { return }
61-
guard let tokenToVerify = token ?? user?.token else { return }
60+
if token == nil && user == nil {
61+
completion(.failure(WFError.couldNotComplete))
62+
return
63+
}
64+
guard let tokenToVerify = token ?? user?.token else {
65+
completion(.failure(WFError.couldNotComplete))
66+
return
67+
}
6268

63-
guard let url = URL(string: "collections", relativeTo: requestURL) else { return }
69+
guard let url = URL(string: "collections", relativeTo: requestURL) else {
70+
completion(.failure(WFError.couldNotComplete))
71+
return
72+
}
6473
var request = URLRequest(url: url)
6574

6675
request.httpMethod = "POST"
@@ -114,10 +123,19 @@ public class WFClient {
114123
withAlias alias: String,
115124
completion: @escaping (Result<WFCollection, Error>) -> Void
116125
) {
117-
if token == nil && user == nil { return }
118-
guard let tokenToVerify = token ?? user?.token else { return }
126+
if token == nil && user == nil {
127+
completion(.failure(WFError.couldNotComplete))
128+
return
129+
}
130+
guard let tokenToVerify = token ?? user?.token else {
131+
completion(.failure(WFError.couldNotComplete))
132+
return
133+
}
119134

120-
guard let url = URL(string: "collections/\(alias)", relativeTo: requestURL) else { return }
135+
guard let url = URL(string: "collections/\(alias)", relativeTo: requestURL) else {
136+
completion(.failure(WFError.couldNotComplete))
137+
return
138+
}
121139
var request = URLRequest(url: url)
122140

123141
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -151,10 +169,19 @@ public class WFClient {
151169
withAlias alias: String,
152170
completion: @escaping (Result<Bool, Error>) -> Void
153171
) {
154-
if token == nil && user == nil { return }
155-
guard let tokenToVerify = token ?? user?.token else { return }
172+
if token == nil && user == nil {
173+
completion(.failure(WFError.couldNotComplete))
174+
return
175+
}
176+
guard let tokenToVerify = token ?? user?.token else {
177+
completion(.failure(WFError.couldNotComplete))
178+
return
179+
}
156180

157-
guard let url = URL(string: "collections/\(alias)", relativeTo: requestURL) else { return }
181+
guard let url = URL(string: "collections/\(alias)", relativeTo: requestURL) else {
182+
completion(.failure(WFError.couldNotComplete))
183+
return
184+
}
158185
var request = URLRequest(url: url)
159186

160187
request.httpMethod = "DELETE"
@@ -190,9 +217,15 @@ public class WFClient {
190217
in collectionAlias: String? = nil,
191218
completion: @escaping (Result<[WFPost], Error>) -> Void
192219
) {
193-
if token == nil && user == nil { return }
220+
if token == nil && user == nil {
221+
completion(.failure(WFError.couldNotComplete))
222+
return
223+
}
194224

195-
guard let tokenToVerify = token ?? user?.token else { return }
225+
guard let tokenToVerify = token ?? user?.token else {
226+
completion(.failure(WFError.couldNotComplete))
227+
return
228+
}
196229

197230
var path = ""
198231
if let alias = collectionAlias {
@@ -201,7 +234,10 @@ public class WFClient {
201234
} else {
202235
path = "me/posts"
203236
}
204-
guard let url = URL(string: path, relativeTo: requestURL) else { return }
237+
guard let url = URL(string: path, relativeTo: requestURL) else {
238+
completion(.failure(WFError.couldNotComplete))
239+
return
240+
}
205241
var request = URLRequest(url: url)
206242

207243
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -249,18 +285,30 @@ public class WFClient {
249285
to collectionAlias: String?,
250286
completion: @escaping (Result<Bool, Error>) -> Void
251287
) {
252-
if token == nil && user == nil { return }
253-
guard let tokenToVerify = token ?? user?.token else { return }
288+
if token == nil && user == nil {
289+
completion(.failure(WFError.couldNotComplete))
290+
return
291+
}
292+
guard let tokenToVerify = token ?? user?.token else {
293+
completion(.failure(WFError.couldNotComplete))
294+
return
295+
}
254296

255-
if collectionAlias == nil && modifyToken != nil { completion(.failure(WFError.badRequest)) }
297+
if collectionAlias == nil && modifyToken != nil {
298+
completion(.failure(WFError.badRequest))
299+
return
300+
}
256301

257302
var urlString = ""
258303
if let collectionAlias = collectionAlias {
259304
urlString = "collections/\(collectionAlias)/collect"
260305
} else {
261306
urlString = "posts/disperse"
262307
}
263-
guard let url = URL(string: urlString, relativeTo: requestURL) else { return }
308+
guard let url = URL(string: urlString, relativeTo: requestURL) else {
309+
completion(.failure(WFError.couldNotComplete))
310+
return
311+
}
264312
var request = URLRequest(url: url)
265313

266314
request.httpMethod = "POST"
@@ -309,10 +357,19 @@ public class WFClient {
309357
in collectionAlias: String,
310358
completion: @escaping (Result<Bool, Error>) -> Void
311359
) {
312-
if token == nil && user == nil { return }
313-
guard let tokenToVerify = token ?? user?.token else { return }
360+
if token == nil && user == nil {
361+
completion(.failure(WFError.couldNotComplete))
362+
return
363+
}
364+
guard let tokenToVerify = token ?? user?.token else {
365+
completion(.failure(WFError.couldNotComplete))
366+
return
367+
}
314368

315-
guard let url = URL(string: "collections/\(collectionAlias)/pin", relativeTo: requestURL) else { return }
369+
guard let url = URL(string: "collections/\(collectionAlias)/pin", relativeTo: requestURL) else {
370+
completion(.failure(WFError.couldNotComplete))
371+
return
372+
}
316373
var request = URLRequest(url: url)
317374

318375
request.httpMethod = "POST"
@@ -367,10 +424,19 @@ public class WFClient {
367424
from collectionAlias: String,
368425
completion: @escaping (Result<Bool, Error>) -> Void
369426
) {
370-
if token == nil && user == nil { return }
371-
guard let tokenToVerify = token ?? user?.token else { return }
427+
if token == nil && user == nil {
428+
completion(.failure(WFError.couldNotComplete))
429+
return
430+
}
431+
guard let tokenToVerify = token ?? user?.token else {
432+
completion(.failure(WFError.couldNotComplete))
433+
return
434+
}
372435

373-
guard let url = URL(string: "collections/\(collectionAlias)/unpin", relativeTo: requestURL) else { return }
436+
guard let url = URL(string: "collections/\(collectionAlias)/unpin", relativeTo: requestURL) else {
437+
completion(.failure(WFError.couldNotComplete))
438+
return
439+
}
374440
var request = URLRequest(url: url)
375441

376442
request.httpMethod = "POST"
@@ -415,8 +481,14 @@ public class WFClient {
415481
in collectionAlias: String? = nil,
416482
completion: @escaping (Result<WFPost, Error>) -> Void
417483
) {
418-
if token == nil && user == nil { return }
419-
guard let tokenToVerify = token ?? user?.token else { return }
484+
if token == nil && user == nil {
485+
completion(.failure(WFError.couldNotComplete))
486+
return
487+
}
488+
guard let tokenToVerify = token ?? user?.token else {
489+
completion(.failure(WFError.couldNotComplete))
490+
return
491+
}
420492

421493
var path = ""
422494
if let alias = collectionAlias {
@@ -425,7 +497,10 @@ public class WFClient {
425497
} else {
426498
path = "posts"
427499
}
428-
guard let url = URL(string: path, relativeTo: requestURL) else { return }
500+
guard let url = URL(string: path, relativeTo: requestURL) else {
501+
completion(.failure(WFError.couldNotComplete))
502+
return
503+
}
429504
var request = URLRequest(url: url)
430505

431506
request.httpMethod = "POST"
@@ -482,10 +557,19 @@ public class WFClient {
482557
byId postId: String,
483558
completion: @escaping (Result<WFPost, Error>) -> Void
484559
) {
485-
if token == nil && user == nil { return }
486-
guard let tokenToVerify = token ?? user?.token else { return }
560+
if token == nil && user == nil {
561+
completion(.failure(WFError.couldNotComplete))
562+
return
563+
}
564+
guard let tokenToVerify = token ?? user?.token else {
565+
completion(.failure(WFError.couldNotComplete))
566+
return
567+
}
487568

488-
guard let url = URL(string: "posts/\(postId)", relativeTo: requestURL) else { return }
569+
guard let url = URL(string: "posts/\(postId)", relativeTo: requestURL) else {
570+
completion(.failure(WFError.couldNotComplete))
571+
return
572+
}
489573
var request = URLRequest(url: url)
490574

491575
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -524,10 +608,17 @@ public class WFClient {
524608
from collectionAlias: String,
525609
completion: @escaping (Result<WFPost, Error>) -> Void
526610
) {
527-
if token == nil && user == nil { return }
528-
guard let tokenToVerify = token ?? user?.token else { return }
611+
if token == nil && user == nil {
612+
completion(.failure(WFError.couldNotComplete))
613+
return
614+
}
615+
guard let tokenToVerify = token ?? user?.token else {
616+
completion(.failure(WFError.couldNotComplete))
617+
return
618+
}
529619

530620
guard let url = URL(string: "collections/\(collectionAlias)/posts/\(slug)", relativeTo: requestURL) else {
621+
completion(.failure(WFError.couldNotComplete))
531622
return
532623
}
533624
var request = URLRequest(url: url)
@@ -569,10 +660,19 @@ public class WFClient {
569660
with modifyToken: String? = nil,
570661
completion: @escaping (Result<WFPost, Error>) -> Void
571662
) {
572-
if token == nil && user == nil { return }
573-
guard let tokenToVerify = token ?? user?.token else { return }
663+
if token == nil && user == nil {
664+
completion(.failure(WFError.couldNotComplete))
665+
return
666+
}
667+
guard let tokenToVerify = token ?? user?.token else {
668+
completion(.failure(WFError.couldNotComplete))
669+
return
670+
}
574671

575-
guard let url = URL(string: "posts/\(postId)", relativeTo: requestURL) else { return }
672+
guard let url = URL(string: "posts/\(postId)", relativeTo: requestURL) else {
673+
completion(.failure(WFError.couldNotComplete))
674+
return
675+
}
576676
var request = URLRequest(url: url)
577677

578678
request.httpMethod = "POST"
@@ -624,10 +724,19 @@ public class WFClient {
624724
with modifyToken: String? = nil,
625725
completion: @escaping (Result<Bool, Error>) -> Void
626726
) {
627-
if token == nil && user == nil { return }
628-
guard let tokenToVerify = token ?? user?.token else { return }
727+
if token == nil && user == nil {
728+
completion(.failure(WFError.couldNotComplete))
729+
return
730+
}
731+
guard let tokenToVerify = token ?? user?.token else {
732+
completion(.failure(WFError.couldNotComplete))
733+
return
734+
}
629735

630-
guard let url = URL(string: "posts/\(postId)", relativeTo: requestURL) else { return }
736+
guard let url = URL(string: "posts/\(postId)", relativeTo: requestURL) else {
737+
completion(.failure(WFError.couldNotComplete))
738+
return
739+
}
631740
var request = URLRequest(url: url)
632741

633742
request.httpMethod = "DELETE"
@@ -666,7 +775,10 @@ public class WFClient {
666775
/// - password: The user's password.
667776
/// - completion: A handler for the `WFUser` object returned on success, or `Error` on failure.
668777
public func login(username: String, password: String, completion: @escaping (Result<WFUser, Error>) -> Void) {
669-
guard let url = URL(string: "auth/login", relativeTo: requestURL) else { return }
778+
guard let url = URL(string: "auth/login", relativeTo: requestURL) else {
779+
completion(.failure(WFError.couldNotComplete))
780+
return
781+
}
670782
var request = URLRequest(url: url)
671783

672784
request.httpMethod = "POST"
@@ -705,11 +817,20 @@ public class WFClient {
705817
/// - token: The token to invalidate.
706818
/// - completion: A handler for the `Bool` object returned on success, or `Error` on failure.
707819
public func logout(token: String? = nil, completion: @escaping (Result<Bool, Error>) -> Void) {
708-
if token == nil && user == nil { return }
820+
if token == nil && user == nil {
821+
completion(.failure(WFError.couldNotComplete))
822+
return
823+
}
709824

710-
guard let tokenToDelete = token ?? user?.token else { return }
825+
guard let tokenToDelete = token ?? user?.token else {
826+
completion(.failure(WFError.couldNotComplete))
827+
return
828+
}
711829

712-
guard let url = URL(string: "auth/me", relativeTo: requestURL) else { fatalError() }
830+
guard let url = URL(string: "auth/me", relativeTo: requestURL) else {
831+
completion(.failure(WFError.couldNotComplete))
832+
return
833+
}
713834
var request = URLRequest(url: url)
714835

715836
request.httpMethod = "DELETE"
@@ -733,11 +854,20 @@ public class WFClient {
733854
/// - token: The access token for the user to fetch.
734855
/// - completion: A handler for the `Data` object returned on success, or `Error` on failure.
735856
public func getUserData(token: String? = nil, completion: @escaping (Result<Data, Error>) -> Void) {
736-
if token == nil && user == nil { return }
857+
if token == nil && user == nil {
858+
completion(.failure(WFError.couldNotComplete))
859+
return
860+
}
737861

738-
guard let tokenToVerify = token ?? user?.token else { return }
862+
guard let tokenToVerify = token ?? user?.token else {
863+
completion(.failure(WFError.couldNotComplete))
864+
return
865+
}
739866

740-
guard let url = URL(string: "me", relativeTo: requestURL) else { return }
867+
guard let url = URL(string: "me", relativeTo: requestURL) else {
868+
completion(.failure(WFError.couldNotComplete))
869+
return
870+
}
741871
var request = URLRequest(url: url)
742872

743873
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -759,11 +889,20 @@ public class WFClient {
759889
/// - token: The access token for the user whose collections are to be retrieved.
760890
/// - completion: A handler for the `[WFCollection]` object returned on success, or `Error` on failure.
761891
public func getUserCollections(token: String? = nil, completion: @escaping (Result<[WFCollection], Error>) -> Void) {
762-
if token == nil && user == nil { return }
892+
if token == nil && user == nil {
893+
completion(.failure(WFError.couldNotComplete))
894+
return
895+
}
763896

764-
guard let tokenToVerify = token ?? user?.token else { return }
897+
guard let tokenToVerify = token ?? user?.token else {
898+
completion(.failure(WFError.couldNotComplete))
899+
return
900+
}
765901

766-
guard let url = URL(string: "me/collections", relativeTo: requestURL) else { return }
902+
guard let url = URL(string: "me/collections", relativeTo: requestURL) else {
903+
completion(.failure(WFError.couldNotComplete))
904+
return
905+
}
767906
var request = URLRequest(url: url)
768907

769908
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -789,10 +928,8 @@ private extension WFClient {
789928
func translateWFError(fromServerResponse response: Data) -> WFError? {
790929
do {
791930
let error = try self.decoder.decode(ErrorMessage.self, from: response)
792-
print("⛔️ \(error.message)")
793931
return WFError(rawValue: error.code)
794932
} catch {
795-
print("⛔️ An unknown error occurred.")
796933
return WFError.unknownError
797934
}
798935
}

0 commit comments

Comments
 (0)