Skip to content

Commit bf5495b

Browse files
committed
A better list of silenceable network errors; also typo in the name Silenceable
1 parent 6a04c6a commit bf5495b

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

AsyncMux/Sources/AsyncError.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,36 @@ public struct HTTPError: LocalizedError {
2121
}
2222

2323

24-
public struct SilencableError: LocalizedError {
24+
public struct SilenceableError: LocalizedError {
2525
let wrapped: Error?
2626

2727
public init(wrapped: Error?) {
2828
self.wrapped = wrapped
2929
}
3030

3131
public var errorDescription: String? {
32-
wrapped?.localizedDescription ?? "SilencableError"
32+
wrapped?.localizedDescription ?? "SilenceableError"
3333
}
3434
}
3535

3636

3737
public extension Error {
3838

39-
var isSilencable: Bool {
39+
var isSilenceable: Bool {
4040
if (self as NSError).domain == NSURLErrorDomain {
41-
return [NSURLErrorNotConnectedToInternet, NSURLErrorNetworkConnectionLost, NSURLErrorCannotConnectToHost].contains((self as NSError).code)
41+
// No connection?
42+
return [
43+
NSURLErrorCannotFindHost,
44+
NSURLErrorCannotConnectToHost,
45+
NSURLErrorNetworkConnectionLost,
46+
NSURLErrorNotConnectedToInternet,
47+
NSURLErrorSecureConnectionFailed,
48+
NSURLErrorBackgroundSessionInUseByAnotherProcess
49+
].contains((self as NSError).code)
4250
}
43-
if self is SilencableError {
44-
return true
51+
else {
52+
// Otherwise, user-defined silenceable error?
53+
return self is SilenceableError
4554
}
46-
return false
4755
}
4856
}

AsyncMux/Sources/Multiplexer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public final class Multiplexer<T: Codable & Sendable>: MuxRepositoryProtocol {
131131
return try await onFetch()
132132
}
133133
catch {
134-
if error.isSilencable {
134+
if error.isSilenceable {
135135
if let storedValue {
136136
return storedValue
137137
}

AsyncMuxDemo/Sources/WeatherAPI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ final class WeatherAPI {
5151
}
5252
catch {
5353
print("ERROR:", error)
54-
// When there's no connection CoreLocation returns the below error; we convert it to a silencable one
54+
// When there's no connection CoreLocation returns the below error; we convert it to a silenceable one
5555
if (error as NSError).domain == kCLErrorDomain, (error as NSError).code == 2 {
56-
throw SilencableError(wrapped: error)
56+
throw SilenceableError(wrapped: error)
5757
}
5858
throw error
5959
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ let myProfile = Multiplexer<UserProfile>(cacheKey: "MyProfile") {
9090
}
9191
```
9292

93-
The objects stored on disk can be reused by the multiplexer even after TTL expires if your `onFetch` fails due to a connectivity problem. You can additionally tell the multiplexer to ignore the error and fetch the cached object by throwing a `SilencableError` in your `onFetch` method.
93+
The objects stored on disk can be reused by the multiplexer even after TTL expires if your `onFetch` fails due to a connectivity problem. You can additionally tell the multiplexer to ignore the error and fetch the cached object by throwing a `SilenceableError` in your `onFetch` method.
9494

9595
The disk storage method is currently hardcoded but will be possible to override in the future releases of the library.
9696

9797
At run time, you can invalidate the cached object using one of the following methods:
9898

99-
- "Soft refresh": chain the `refresh()` method with a call to `request()`: the multiplexer will attempt to fetch the object again, but will not discard the existing cached objects in memory or on disk. In case of a silencable error (i.e. connectivity issue) the older cached object will be used again as a result.
99+
- "Soft refresh": chain the `refresh()` method with a call to `request()`: the multiplexer will attempt to fetch the object again, but will not discard the existing cached objects in memory or on disk. In case of a silenceable error (i.e. connectivity issue) the older cached object will be used again as a result.
100100
- "Hard refresh": call `clear()` to discard both memory and disk caches for a given object. The next call to `request()` will attempt to fetch the object and will fail in case of an error.
101101

102102
More detailed descriptions on each method can be found in the source file [Multiplexer.swift](AsyncMux/Sources/Multiplexer.swift).

0 commit comments

Comments
 (0)