Skip to content

Commit e988f80

Browse files
committed
Misc changes
1 parent 4d3c66a commit e988f80

File tree

9 files changed

+44
-63
lines changed

9 files changed

+44
-63
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. Take a look
1111
#### Navigator
1212

1313
* The `AudioNavigator` API has been promoted to stable and ships with a new Preferences API.
14+
* The new `NavigatorDelegate.didFailToLoadResourceAt(_:didFailToLoadResourceAt:withError:)` delegate API notifies when an error occurs while loading a publication resource (contributed by [@ettore](https://github.com/readium/swift-toolkit/pull/400)).
1415

1516
### Fixed
1617

Sources/Adapters/GCDWebServer/GCDHTTPServer.swift

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ public class GCDHTTPServer: HTTPServer, Loggable {
172172

173173
// MARK: HTTPServer
174174

175+
public func serve(at endpoint: HTTPServerEndpoint, handler: @escaping (HTTPServerRequest) -> any Resource) throws -> URL {
176+
try serve(at: endpoint, handler: handler, failureHandler: nil)
177+
}
178+
175179
public func serve(
176180
at endpoint: HTTPServerEndpoint,
177181
handler: @escaping (HTTPServerRequest) -> Resource,

Sources/Navigator/CBZ/CBZNavigatorViewController.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,10 @@ open class CBZNavigatorViewController: UIViewController, VisualNavigator, Loggab
6161
publication: publication,
6262
failureHandler: { [weak self] request, error in
6363
DispatchQueue.main.async { [weak self] in
64-
guard let self = self else {
64+
guard let self = self, let href = request.href else {
6565
return
6666
}
67-
self.delegate?.navigator(self,
68-
didFailToLoadResourceAt: request.href,
69-
url: request.url,
70-
withError: error)
67+
self.delegate?.navigator(self, didFailToLoadResourceAt: href, withError: error)
7168
}
7269
}
7370
)

Sources/Navigator/EPUB/EPUBNavigatorViewController.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -947,15 +947,11 @@ extension EPUBNavigatorViewController: EPUBNavigatorViewModelDelegate {
947947

948948
func epubNavigatorViewModel(
949949
_ viewModel: EPUBNavigatorViewModel,
950-
didFailToLoadResourceAt href: String?,
951-
url: URL,
950+
didFailToLoadResourceAt href: String,
952951
withError error: ResourceError
953952
) {
954953
DispatchQueue.main.async {
955-
self.delegate?.navigator(self,
956-
didFailToLoadResourceAt: href,
957-
url: url,
958-
withError: error)
954+
self.delegate?.navigator(self, didFailToLoadResourceAt: href, withError: error)
959955
}
960956
}
961957
}

Sources/Navigator/EPUB/EPUBNavigatorViewModel.swift

+3-9
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import UIKit
1111
protocol EPUBNavigatorViewModelDelegate: AnyObject {
1212
func epubNavigatorViewModel(_ viewModel: EPUBNavigatorViewModel, runScript script: String, in scope: EPUBScriptScope)
1313
func epubNavigatorViewModelInvalidatePaginationView(_ viewModel: EPUBNavigatorViewModel)
14-
func epubNavigatorViewModel(_ viewModel: EPUBNavigatorViewModel,
15-
didFailToLoadResourceAt href: String?,
16-
url: URL,
17-
withError error: ResourceError)
14+
func epubNavigatorViewModel(_ viewModel: EPUBNavigatorViewModel, didFailToLoadResourceAt href: String, withError error: ResourceError)
1815
}
1916

2017
enum EPUBScriptScope {
@@ -83,13 +80,10 @@ final class EPUBNavigatorViewModel: Loggable {
8380
at: uuidEndpoint, // serving the chapters endpoint
8481
publication: publication,
8582
failureHandler: { [weak self] request, error in
86-
guard let self = self else {
83+
guard let self = self, let href = request.href else {
8784
return
8885
}
89-
self.delegate?.epubNavigatorViewModel(self,
90-
didFailToLoadResourceAt: request.href,
91-
url: request.url,
92-
withError: error)
86+
self.delegate?.epubNavigatorViewModel(self, didFailToLoadResourceAt: href, withError: error)
9387
}
9488
)
9589
}

Sources/Navigator/Navigator.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public protocol NavigatorDelegate: AnyObject {
105105
func navigator(_ navigator: Navigator, shouldNavigateToNoteAt link: Link, content: String, referrer: String?) -> Bool
106106

107107
/// Called when an error occurs while attempting to load a resource.
108-
func navigator(_ navigator: Navigator, didFailToLoadResourceAt href: String?, url: URL, withError error: ResourceError)
108+
func navigator(_ navigator: Navigator, didFailToLoadResourceAt href: String, withError error: ResourceError)
109109
}
110110

111111
public extension NavigatorDelegate {
@@ -121,7 +121,7 @@ public extension NavigatorDelegate {
121121
true
122122
}
123123

124-
func navigator(_ navigator: Navigator, didFailToLoadResourceAt href: String?, url: URL, withError error: ResourceError) {}
124+
func navigator(_ navigator: Navigator, didFailToLoadResourceAt href: String, withError error: ResourceError) {}
125125
}
126126

127127
public enum NavigatorError: LocalizedError {

Sources/Navigator/PDF/PDFNavigatorViewController.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,10 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
122122
publication: publication,
123123
failureHandler: { request, error in
124124
DispatchQueue.main.async { [weak self] in
125-
guard let self = self else {
125+
guard let self = self, let href = request.href else {
126126
return
127127
}
128-
self.delegate?.navigator(self,
129-
didFailToLoadResourceAt: request.href,
130-
url: request.url,
131-
withError: error)
128+
self.delegate?.navigator(self, didFailToLoadResourceAt: href, withError: error)
132129
}
133130
}
134131
)

Sources/Shared/Toolkit/HTTP/HTTPServer.swift

+24-36
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public protocol HTTPServer {
1919
///
2020
/// - Returns the base URL for this endpoint.
2121
@discardableResult
22-
func serve(at endpoint: HTTPServerEndpoint,
23-
handler: @escaping (HTTPServerRequest) -> Resource) throws -> URL
22+
func serve(
23+
at endpoint: HTTPServerEndpoint,
24+
handler: @escaping (HTTPServerRequest) -> Resource
25+
) throws -> URL
2426

2527
/// Serves resources at the given `endpoint`.
2628
///
@@ -30,9 +32,11 @@ public protocol HTTPServer {
3032
///
3133
/// - Returns the base URL for this endpoint.
3234
@discardableResult
33-
func serve(at endpoint: HTTPServerEndpoint,
34-
handler: @escaping (HTTPServerRequest) -> Resource,
35-
failureHandler: FailureHandler?) throws -> URL
35+
func serve(
36+
at endpoint: HTTPServerEndpoint,
37+
handler: @escaping (HTTPServerRequest) -> Resource,
38+
failureHandler: FailureHandler?
39+
) throws -> URL
3640

3741
/// Registers a `Resource` transformer that will be run on all responses
3842
/// matching the given `endpoint`.
@@ -47,21 +51,10 @@ public extension HTTPServer {
4751
@discardableResult
4852
func serve(
4953
at endpoint: HTTPServerEndpoint,
50-
handler: @escaping (HTTPServerRequest) -> Resource
54+
handler: @escaping (HTTPServerRequest) -> Resource,
55+
failureHandler: FailureHandler?
5156
) throws -> URL {
52-
try serve(at: endpoint, handler: handler, failureHandler: nil)
53-
}
54-
55-
/// Serves the local file `url` at the given `endpoint`.
56-
///
57-
/// If the provided `url` is a directory, then all the files in the
58-
/// directory are served. Subsequent calls with the same served `endpoint`
59-
/// overwrite each other.
60-
///
61-
/// - Returns the URL to access the file(s) on the server.
62-
@discardableResult
63-
func serve(at endpoint: HTTPServerEndpoint, contentsOf url: URL) throws -> URL {
64-
try serve(at: endpoint, contentsOf: url, failureHandler: nil)
57+
try serve(at: endpoint, handler: handler)
6558
}
6659

6760
/// Serves the local file `url` at the given `endpoint`.
@@ -77,7 +70,7 @@ public extension HTTPServer {
7770
func serve(
7871
at endpoint: HTTPServerEndpoint,
7972
contentsOf url: URL,
80-
failureHandler: FailureHandler?
73+
failureHandler: FailureHandler? = nil
8174
) throws -> URL {
8275
func handler(request: HTTPServerRequest) -> Resource {
8376
let file = url.appendingPathComponent(request.href ?? "")
@@ -91,18 +84,11 @@ public extension HTTPServer {
9184
)
9285
}
9386

94-
return try serve(at: endpoint,
95-
handler: handler(request:),
96-
failureHandler: failureHandler)
97-
}
98-
99-
/// Serves a `publication`'s resources at the given `endpoint`.
100-
///
101-
/// - Returns the base URL to access the publication's resources on the
102-
/// server.
103-
@discardableResult
104-
func serve(at endpoint: HTTPServerEndpoint, publication: Publication) throws -> URL {
105-
try serve(at: endpoint, publication: publication, failureHandler: nil)
87+
return try serve(
88+
at: endpoint,
89+
handler: handler(request:),
90+
failureHandler: failureHandler
91+
)
10692
}
10793

10894
/// Serves a `publication`'s resources at the given `endpoint`.
@@ -115,7 +101,7 @@ public extension HTTPServer {
115101
func serve(
116102
at endpoint: HTTPServerEndpoint,
117103
publication: Publication,
118-
failureHandler: FailureHandler?
104+
failureHandler: FailureHandler? = nil
119105
) throws -> URL {
120106
func handler(request: HTTPServerRequest) -> Resource {
121107
guard let href = request.href else {
@@ -130,9 +116,11 @@ public extension HTTPServer {
130116
return publication.get(href)
131117
}
132118

133-
return try serve(at: endpoint,
134-
handler: handler(request:),
135-
failureHandler: failureHandler)
119+
return try serve(
120+
at: endpoint,
121+
handler: handler(request:),
122+
failureHandler: failureHandler
123+
)
136124
}
137125
}
138126

TestApp/Sources/Reader/Common/ReaderViewController.swift

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class ReaderViewController<N: Navigator>: UIViewController,
107107
moduleDelegate?.presentError(error, from: self)
108108
}
109109

110+
func navigator(_ navigator: any Navigator, didFailToLoadResourceAt href: String, withError error: ResourceError) {
111+
log(.error, "Failed to load resource at \(href): \(error)")
112+
}
113+
110114
// MARK: - Locations
111115

112116
var currentBookmark: Bookmark? {

0 commit comments

Comments
 (0)