Skip to content

Commit

Permalink
Add proper error message for ATS (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka authored Jun 14, 2024
1 parent ea9dc8c commit 09df06f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 55 deletions.
1 change: 1 addition & 0 deletions Zotero/Assets/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@
"errors.settings.webdav.forbidden" = "You don’t have permission to access the specified folder on the WebDAV server.";
"errors.settings.webdav.internet_connection" = "Unable to connect to the network. Please try again.";
"errors.settings.webdav.host_not_found" = "Could not connect to WebDAV server";
"errors.settings.webdav.ats" = "Due to iOS restrictions, you must use HTTPS to connect to a non-local WebDAV server.";
"errors.shareext.logged_out" = "Please log into the app before using this extension.";
"errors.shareext.cant_load_data" = "Failed to load data. Please try again.";
"errors.shareext.unknown" = "An unknown error occurred.";
Expand Down
60 changes: 60 additions & 0 deletions Zotero/Controllers/WebDAV/WebDavController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,66 @@ enum WebDavError {
case cantCreatePropData
case apiError(error: AFError, httpMethod: String?)
}

static func message(for error: Error) -> String {
if let error = error as? WebDavError.Verification {
return error.message
}

if let responseError = error as? AFResponseError, let message = errorMessage(for: responseError.error) {
return message
}

if let error = error as? AFError, let message = errorMessage(for: error) {
return message
}

return error.localizedDescription

func errorMessage(for error: AFError) -> String? {
switch error {
case .sessionTaskFailed(let error):
let nsError = error as NSError
if nsError.domain == NSURLErrorDomain {
switch nsError.code {
case NSURLErrorNotConnectedToInternet:
return L10n.Errors.Settings.Webdav.internetConnection

case NSURLErrorCannotConnectToHost, NSURLErrorTimedOut:
return L10n.Errors.Settings.Webdav.hostNotFound

case NSURLErrorAppTransportSecurityRequiresSecureConnection:
return L10n.Errors.Settings.Webdav.ats

default: break
}
}

case .responseValidationFailed(let reason):
switch reason {
case .unacceptableStatusCode(let statusCode):
switch statusCode {
case 401:
return L10n.Errors.Settings.Webdav.unauthorized

case 403:
return L10n.Errors.Settings.Webdav.forbidden

default:
return nil
}

default:
break
}

default:
break
}

return L10n.Errors.Settings.Webdav.hostNotFound
}
}
}

struct WebDavDeletionResult {
Expand Down
2 changes: 2 additions & 0 deletions Zotero/Extensions/Localizable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ internal enum L10n {
/// Could not collect storage data
internal static let storage = L10n.tr("Localizable", "errors.settings.storage", fallback: "Could not collect storage data")
internal enum Webdav {
/// Due to iOS restrictions, you must use HTTPS to connect to a non-local WebDAV server.
internal static let ats = L10n.tr("Localizable", "errors.settings.webdav.ats", fallback: "Due to iOS restrictions, you must use HTTPS to connect to a non-local WebDAV server.")
/// A potential problem was found with your WebDAV server.
///
/// An uploaded file was not immediately available for download. There may be a short delay between when you upload files and when they become available, particularly if you are using a cloud storage service.
Expand Down
8 changes: 5 additions & 3 deletions Zotero/Scenes/General/Views/SyncToolbarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ final class SyncToolbarController {
break

case .apiError(let error, let httpMethod):
guard let statusCode = error.unacceptableStatusCode else { break }
return (L10n.Errors.SyncToolbar.webdavRequestFailed(statusCode, httpMethod ?? "Unknown"), nil)
if let statusCode = error.unacceptableStatusCode {
return (L10n.Errors.SyncToolbar.webdavRequestFailed(statusCode, httpMethod ?? "Unknown"), nil)
}
return (WebDavError.message(for: error), nil)
}

case .annotationDidSplit(let string, let keys, let libraryId):
Expand All @@ -241,7 +243,7 @@ final class SyncToolbarController {
}
}

return ("", nil)
return (error.localizedDescription, nil)
}

private func setToolbar(hidden: Bool, animated: Bool) {
Expand Down
53 changes: 1 addition & 52 deletions Zotero/Scenes/Master/Settings/Sync/Views/SyncSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,65 +148,14 @@ struct WebDavSettings: View {
}

if case .failure(let error) = self.viewModel.state.webDavVerificationResult {
Text(self.errorMessage(for: error))
Text(WebDavError.message(for: error))
.foregroundColor(.red)
}
}

private var canVerifyServer: Bool {
return !self.viewModel.state.url.isEmpty && !self.viewModel.state.username.isEmpty && !self.viewModel.state.password.isEmpty
}

private func errorMessage(for error: Error) -> String {
if let error = error as? WebDavError.Verification {
return error.message
}

if let responseError = error as? AFResponseError, let message = self.errorMessage(for: responseError.error) {
return message
}
if let error = error as? AFError, let message = self.errorMessage(for: error) {
return message
}

return error.localizedDescription
}

private func errorMessage(for error: AFError) -> String? {
switch error {
case .sessionTaskFailed(let error):
let nsError = error as NSError
if nsError.domain == NSURLErrorDomain {
switch nsError.code {
case NSURLErrorNotConnectedToInternet:
return L10n.Errors.Settings.Webdav.internetConnection

case NSURLErrorCannotConnectToHost, NSURLErrorTimedOut:
return L10n.Errors.Settings.Webdav.hostNotFound
default: break
}
}

case .responseValidationFailed(let reason):
switch reason {
case .unacceptableStatusCode(let statusCode):
switch statusCode {
case 401:
return L10n.Errors.Settings.Webdav.unauthorized

case 403:
return L10n.Errors.Settings.Webdav.forbidden
default: return nil
}

default: break
}

default: break
}

return L10n.Errors.Settings.Webdav.hostNotFound
}
}

struct ProfileView_Previews: PreviewProvider {
Expand Down

0 comments on commit 09df06f

Please sign in to comment.