diff --git a/Shared/Generated/Strings.swift b/Shared/Generated/Strings.swift index e62b4404..ccaf4c22 100644 --- a/Shared/Generated/Strings.swift +++ b/Shared/Generated/Strings.swift @@ -354,6 +354,8 @@ internal enum L10n { internal static let system = L10n.tr("Localizable", "system") /// Tags internal static let tags = L10n.tr("Localizable", "tags") + /// Too Many Redirects + internal static let tooManyRedirects = L10n.tr("Localizable", "tooManyRedirects") /// Try again internal static let tryAgain = L10n.tr("Localizable", "tryAgain") /// TV Shows diff --git a/Shared/ViewModels/ConnectToServerViewModel.swift b/Shared/ViewModels/ConnectToServerViewModel.swift index aa86ed37..12a5fbea 100644 --- a/Shared/ViewModels/ConnectToServerViewModel.swift +++ b/Shared/ViewModels/ConnectToServerViewModel.swift @@ -44,7 +44,8 @@ final class ConnectToServerViewModel: ViewModel { return message } - func connectToServer(uri: String) { + func connectToServer(uri: String, redirectCount: Int = 0) { + #if targetEnvironment(simulator) var uri = uri if uri == "localhost" { @@ -63,6 +64,28 @@ final class ConnectToServerViewModel: ViewModel { case .finished: () case let .failure(error): switch error { + case is ErrorResponse: + let errorResponse = error as! ErrorResponse + switch errorResponse { + case let .error(_, _, response, _): + // a url in the response is the result if a redirect + if let newURL = response?.url { + if redirectCount > 2 { + self.handleAPIRequestError(displayMessage: L10n.tooManyRedirects, + logLevel: .critical, + tag: "connectToServer", + completion: completion) + } else { + self + .connectToServer(uri: newURL.absoluteString + .removeRegexMatches(pattern: "/web/index.html", replaceWith: ""), + redirectCount: redirectCount + 1) + } + } else { + self.handleAPIRequestError(completion: completion) + } + } + print(errorResponse) case is SwiftfinStore.Errors: let swiftfinError = error as! SwiftfinStore.Errors switch swiftfinError { diff --git a/Translations/en.lproj/Localizable.strings b/Translations/en.lproj/Localizable.strings index cc09b41e..a1457c89 100644 Binary files a/Translations/en.lproj/Localizable.strings and b/Translations/en.lproj/Localizable.strings differ