Skip to content

Commit

Permalink
IOS-631 Fix crash when Publication::baseURL is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
ettore committed Feb 1, 2024
1 parent a6e4879 commit a8af695
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
11 changes: 6 additions & 5 deletions Simplified/Reader2/ReaderPresentation/ReaderError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions Simplified/Reader2/UI/NYPLEPUBViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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?,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Simplified/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion Simplified/it.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit a8af695

Please sign in to comment.