From a8af695ea2874c4fcc92ca367cfbd0137a4bab42 Mon Sep 17 00:00:00 2001 From: Ettore Pasquini Date: Thu, 1 Feb 2024 15:46:09 -0800 Subject: [PATCH] IOS-631 Fix crash when Publication::baseURL is nil --- .../ReaderPresentation/ReaderError.swift | 11 ++++++----- .../ReadingFormats/EPUBModule.swift | 6 +++++- .../Reader2/UI/NYPLEPUBViewController.swift | 18 +++++++++++++----- Simplified/en.lproj/Localizable.strings | 2 +- Simplified/it.lproj/Localizable.strings | 2 +- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Simplified/Reader2/ReaderPresentation/ReaderError.swift b/Simplified/Reader2/ReaderPresentation/ReaderError.swift index 8964ce221..b15171fb4 100644 --- a/Simplified/Reader2/ReaderPresentation/ReaderError.swift +++ b/Simplified/Reader2/ReaderPresentation/ReaderError.swift @@ -14,15 +14,16 @@ import Foundation enum ReaderError: LocalizedError { case formatNotSupported - case epubNotValid - + case epubNotValid(String) + var errorDescription: String? { switch self { case .formatNotSupported: return NSLocalizedString("The book you were trying to read is in an unsupported format.", comment: "Error message when trying to read a publication with a unsupported format") - case .epubNotValid: - return NSLocalizedString("The book you were trying to read is corrupted. Please try downloading it again.", comment: "Error message when trying to read an EPUB that is invalid") + case .epubNotValid(let errorCause): + return String.localizedStringWithFormat( + NSLocalizedString("The book you were trying to read is corrupted (%@). Please try downloading it again.", comment: "Error message when trying to read an EPUB that is invalid"), + errorCause) } } - } diff --git a/Simplified/Reader2/ReaderPresentation/ReadingFormats/EPUBModule.swift b/Simplified/Reader2/ReaderPresentation/ReadingFormats/EPUBModule.swift index 02f53bd1c..934efe77a 100644 --- a/Simplified/Reader2/ReaderPresentation/ReadingFormats/EPUBModule.swift +++ b/Simplified/Reader2/ReaderPresentation/ReadingFormats/EPUBModule.swift @@ -39,9 +39,13 @@ final class EPUBModule: ReaderFormatModule { initialLocation: Locator?) throws -> UIViewController { guard publication.metadata.identifier != nil else { - throw ReaderError.epubNotValid + throw ReaderError.epubNotValid("nil metadata id") } + guard publication.baseURL != nil else { + throw ReaderError.epubNotValid("nil baseURL") + } + let epubVC = NYPLEPUBViewController(publication: publication, book: book, initialLocation: initialLocation, diff --git a/Simplified/Reader2/UI/NYPLEPUBViewController.swift b/Simplified/Reader2/UI/NYPLEPUBViewController.swift index 4a6cc02ba..0a0baaae6 100644 --- a/Simplified/Reader2/UI/NYPLEPUBViewController.swift +++ b/Simplified/Reader2/UI/NYPLEPUBViewController.swift @@ -40,16 +40,21 @@ fileprivate protocol EPUBNavigatorViewControllerMaking { } fileprivate class EPUBNavigatorViewControllerFactory: EPUBNavigatorViewControllerMaking { + /// - Important: `publication.baseURL` MUST not be nil when invoking this + /// function. @available(*, deprecated, message: "To suppress this warning, cast to EPUBNavigatorViewControllerMaking protocol") fileprivate func make(publication: Publication, initialLocation: Locator?, resourcesServer: ResourcesServer, config: EPUBNavigatorViewController.Configuration) -> EPUBNavigatorViewController { - EPUBNavigatorViewController(publication: publication, - initialLocation: initialLocation, - resourcesServer: resourcesServer, - config: config) + + assert(publication.baseURL != nil, "Publication.baseURL MUST not be nil") + + return EPUBNavigatorViewController(publication: publication, + initialLocation: initialLocation, + resourcesServer: resourcesServer, + config: config) } } @@ -63,6 +68,8 @@ class NYPLEPUBViewController: NYPLBaseReaderViewController { let userSettings: NYPLR1R2UserSettings + /// - Important: `publication.baseURL` MUST not be nil when invoking this + /// initializer. init(publication: Publication, book: NYPLBook, initialLocation: Locator?, @@ -92,7 +99,8 @@ class NYPLEPUBViewController: NYPLBaseReaderViewController { preloadNextPositionCount: 0, debugState: true) - // when changing the type of the navigator, also change `epubNavigator` getter + // create navigator + assert(publication.baseURL != nil, "Publication.baseURL MUST not be nil when creating a EPUBNavigatorViewController") let factory: EPUBNavigatorViewControllerMaking = EPUBNavigatorViewControllerFactory() let navigator = factory.make(publication: publication, initialLocation: initialLocation, diff --git a/Simplified/en.lproj/Localizable.strings b/Simplified/en.lproj/Localizable.strings index 72a4d9ed5..2c7def146 100644 --- a/Simplified/en.lproj/Localizable.strings +++ b/Simplified/en.lproj/Localizable.strings @@ -196,7 +196,7 @@ "The book you were trying to open is invalid." = "The book you were trying to open is invalid."; "An error was encountered while trying to open this book." = "An error was encountered while trying to open this book."; "The book you were trying to read is in an unsupported format." = "The book you were trying to read is in an unsupported format."; -"The book you were trying to read is corrupted. Please try downloading it again." = "The book you were trying to read is corrupted. Please try downloading it again."; +"The book you were trying to read is corrupted (%@). Please try downloading it again." = "The book you were trying to read is corrupted (%@). Please try downloading it again."; // MARK: - Authentication generic "Authentication Expired" = "Authentication Expired"; diff --git a/Simplified/it.lproj/Localizable.strings b/Simplified/it.lproj/Localizable.strings index bf3eedab2..586e7b059 100644 --- a/Simplified/it.lproj/Localizable.strings +++ b/Simplified/it.lproj/Localizable.strings @@ -192,7 +192,7 @@ "The book you were trying to open is invalid." = "Il libro che stavi cercando di leggere è in un formato non valido."; "An error was encountered while trying to open this book." = "Si è verificato un errore durante l'apertura di questo libro."; "The book you were trying to read is in an unsupported format." = "Il libro che stavi cercando di aprire è in un formato non supportato."; -"The book you were trying to read is corrupted. Please try downloading it again." = "Il libro che stavi cercando di leggere è danneggiato. Prova a scaricarlo di nuovo."; +"The book you were trying to read is corrupted (%@). Please try downloading it again." = "Il libro che stavi cercando di leggere è danneggiato (%@). Prova a scaricarlo di nuovo."; // MARK: - Authentication generic "Authentication Expired" = "Credenziali di accesso scadute";