115 lines
3.8 KiB
Swift
115 lines
3.8 KiB
Swift
//
|
|
// Swiftfin is subject to the terms of the Mozilla Public
|
|
// License, v2.0. If a copy of the MPL was not distributed with this
|
|
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
|
//
|
|
// Copyright (c) 2022 Jellyfin & Jellyfin Contributors
|
|
//
|
|
|
|
import Foundation
|
|
import JellyfinAPI
|
|
|
|
enum NetworkError: Error {
|
|
|
|
/// For the case that the ErrorResponse object has a code of -1
|
|
case URLError(response: ErrorResponse, displayMessage: String?)
|
|
|
|
/// For the case that the ErrorRespones object has a code of -2
|
|
case HTTPURLError(response: ErrorResponse, displayMessage: String?)
|
|
|
|
/// For the case that the ErrorResponse object has a positive code
|
|
case JellyfinError(response: ErrorResponse, displayMessage: String?)
|
|
|
|
var errorMessage: ErrorMessage {
|
|
switch self {
|
|
case let .URLError(response, displayMessage):
|
|
return NetworkError.parseURLError(from: response, displayMessage: displayMessage)
|
|
case let .HTTPURLError(response, displayMessage):
|
|
return NetworkError.parseHTTPURLError(from: response, displayMessage: displayMessage)
|
|
case let .JellyfinError(response, displayMessage):
|
|
return NetworkError.parseJellyfinError(from: response, displayMessage: displayMessage)
|
|
}
|
|
}
|
|
|
|
private static func parseURLError(from response: ErrorResponse, displayMessage: String?) -> ErrorMessage {
|
|
let errorMessage: ErrorMessage
|
|
|
|
switch response {
|
|
case let .error(_, _, _, err):
|
|
|
|
// Code references:
|
|
// https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes
|
|
switch err._code {
|
|
case -1001:
|
|
errorMessage = ErrorMessage(
|
|
code: err._code,
|
|
title: L10n.error,
|
|
message: L10n.networkTimedOut
|
|
)
|
|
case -1003:
|
|
errorMessage = ErrorMessage(
|
|
code: err._code,
|
|
title: L10n.error,
|
|
message: L10n.unableToFindHost
|
|
)
|
|
case -1004:
|
|
errorMessage = ErrorMessage(
|
|
code: err._code,
|
|
title: L10n.error,
|
|
message: L10n.cannotConnectToHost
|
|
)
|
|
default:
|
|
errorMessage = ErrorMessage(
|
|
code: err._code,
|
|
title: L10n.error,
|
|
message: L10n.unknownError
|
|
)
|
|
}
|
|
}
|
|
|
|
return errorMessage
|
|
}
|
|
|
|
private static func parseHTTPURLError(from response: ErrorResponse, displayMessage: String?) -> ErrorMessage {
|
|
let errorMessage: ErrorMessage
|
|
|
|
// Not implemented as has not run into one of these errors as time of writing
|
|
switch response {
|
|
case .error:
|
|
errorMessage = ErrorMessage(
|
|
code: 0,
|
|
title: L10n.error,
|
|
message: "An HTTP URL error has occurred"
|
|
)
|
|
}
|
|
|
|
return errorMessage
|
|
}
|
|
|
|
private static func parseJellyfinError(from response: ErrorResponse, displayMessage: String?) -> ErrorMessage {
|
|
let errorMessage: ErrorMessage
|
|
|
|
switch response {
|
|
case let .error(code, _, _, _):
|
|
|
|
// Generic HTTP status codes
|
|
switch code {
|
|
case 401:
|
|
errorMessage = ErrorMessage(
|
|
code: code,
|
|
title: L10n.unauthorized,
|
|
message: L10n.unauthorizedUser
|
|
)
|
|
default:
|
|
errorMessage = ErrorMessage(
|
|
code: code,
|
|
title: L10n.error,
|
|
message: displayMessage ?? L10n.unknownError
|
|
)
|
|
}
|
|
}
|
|
|
|
return errorMessage
|
|
}
|
|
}
|