From f975bc6f2d90087810f4688eff81b2304971072b Mon Sep 17 00:00:00 2001 From: benshutt Date: Thu, 2 Jul 2020 14:52:00 +0100 Subject: [PATCH 1/2] Post `Notification` when a `.notConnectedToInternet` `URLError` is returned as the `Error` of a URL task --- ThunderRequest/RequestController+Callbacks.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ThunderRequest/RequestController+Callbacks.swift b/ThunderRequest/RequestController+Callbacks.swift index b0c102a..8bfd1d5 100644 --- a/ThunderRequest/RequestController+Callbacks.swift +++ b/ThunderRequest/RequestController+Callbacks.swift @@ -18,6 +18,11 @@ extension RequestController { public static let ErrorDomain = "com.threesidedcube.ThunderRequest" + /// `Notification` posted when the `Error` from a URL task + /// is `URLError.code == .notConnectedToInternet` + public static let DidErrorWithNotConnectedToInternetNotificationName = + Notification.Name(rawValue: "TSCDidErrorWithNotConnectedToInternet") + public static let DidReceiveResponseNotificationName = Notification.Name(rawValue: "TSCRequestDidReceiveResponse") public static let DidErrorNotificationName = Notification.Name("TSCRequestServerError") @@ -74,6 +79,10 @@ extension RequestController { NotificationCenter.default.post(name: RequestController.DidReceiveResponseNotificationName, object: nil, userInfo: requestInfo) + if let urlError = error as? URLError, urlError.code == .notConnectedToInternet { + NotificationCenter.default.post(name: RequestController.DidErrorWithNotConnectedToInternetNotificationName, object: nil, userInfo: requestInfo) + } + if response?.status.isConsideredError == true { NotificationCenter.default.post(name: RequestController.DidErrorNotificationName, object: nil, userInfo: requestInfo) } From 6f44bd13ac6de05292b72a27c1b27a5aa4c96a50 Mon Sep 17 00:00:00 2001 From: benshutt Date: Thu, 2 Jul 2020 15:36:58 +0100 Subject: [PATCH 2/2] Extend previous commit to post a `Notification` for all `URLError` responses --- ThunderRequest/RequestController+Callbacks.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ThunderRequest/RequestController+Callbacks.swift b/ThunderRequest/RequestController+Callbacks.swift index 8bfd1d5..302fd81 100644 --- a/ThunderRequest/RequestController+Callbacks.swift +++ b/ThunderRequest/RequestController+Callbacks.swift @@ -12,16 +12,16 @@ import os.log public struct RequestNotificationKey { public static let request = "TSCRequestNotificationRequestKey" public static let response = "TSCRequestNotificationResponseKey" + public static let error = "TSCRequestNotificationErrorKey" } extension RequestController { public static let ErrorDomain = "com.threesidedcube.ThunderRequest" - /// `Notification` posted when the `Error` from a URL task - /// is `URLError.code == .notConnectedToInternet` - public static let DidErrorWithNotConnectedToInternetNotificationName = - Notification.Name(rawValue: "TSCDidErrorWithNotConnectedToInternet") + /// Name of `Notification` posted when the `Error` from a URL task is a `URLError` + public static let RequestDidURLErrorNotificationName = + Notification.Name(rawValue: "TSCRequestDidURLError") public static let DidReceiveResponseNotificationName = Notification.Name(rawValue: "TSCRequestDidReceiveResponse") @@ -79,8 +79,10 @@ extension RequestController { NotificationCenter.default.post(name: RequestController.DidReceiveResponseNotificationName, object: nil, userInfo: requestInfo) - if let urlError = error as? URLError, urlError.code == .notConnectedToInternet { - NotificationCenter.default.post(name: RequestController.DidErrorWithNotConnectedToInternetNotificationName, object: nil, userInfo: requestInfo) + if let urlError = error as? URLError { + var userInfo = requestInfo + userInfo[RequestNotificationKey.error] = urlError + NotificationCenter.default.post(name: RequestController.RequestDidURLErrorNotificationName, object: nil, userInfo: userInfo) } if response?.status.isConsideredError == true {