From b6dca1938bd6eba62bb2b7363b7ee27a01c96b31 Mon Sep 17 00:00:00 2001 From: Oleksandr Kharchenko Date: Mon, 17 Jun 2024 17:38:20 +0300 Subject: [PATCH] [ZEUS-4329] Adde enum for Playback error reasons --- .../PlayBack API/PlayBackAPIService.swift | 8 +-- Sources/PlaybackSDK/PlayBackSDKManager.swift | 49 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Sources/PlaybackSDK/PlayBack API/PlayBackAPIService.swift b/Sources/PlaybackSDK/PlayBack API/PlayBackAPIService.swift index 43bc68e..0fa7ac7 100644 --- a/Sources/PlaybackSDK/PlayBack API/PlayBackAPIService.swift +++ b/Sources/PlaybackSDK/PlayBack API/PlayBackAPIService.swift @@ -67,10 +67,12 @@ internal class PlayBackAPIService: PlayBackAPI { return data default: let decoder = JSONDecoder() - if let errorResponse = try? decoder.decode(PlaybackResponseModel.self, from: data) { - throw PlayBackAPIError.apiError(statusCode: httpResponse.statusCode, message: errorResponse.message ?? "Unknown authentication error message", reason: errorResponse.reason ?? "Unknown authentication error reason") + if let errorResponse = try? decoder.decode(PlaybackResponseModel.self, from: data) { + let errorReason = errorResponse.reason ?? "Unknown authentication error reason" + throw PlayBackAPIError.apiError(statusCode: httpResponse.statusCode, message: errorResponse.message ?? "Unknown authentication error message", reason: PlaybackErrorReason(fromString: errorReason)) } else { - throw PlayBackAPIError.apiError(statusCode: httpResponse.statusCode, message: "Unknown authentication error", reason: "Unknown authentication error reason") + let errorReason = "Unknown authentication error reason" + throw PlayBackAPIError.apiError(statusCode: httpResponse.statusCode, message: "Unknown authentication error", reason: PlaybackErrorReason(fromString: errorReason)) } } } diff --git a/Sources/PlaybackSDK/PlayBackSDKManager.swift b/Sources/PlaybackSDK/PlayBackSDKManager.swift index c105adb..1642ebb 100644 --- a/Sources/PlaybackSDK/PlayBackSDKManager.swift +++ b/Sources/PlaybackSDK/PlayBackSDKManager.swift @@ -18,6 +18,53 @@ public enum SDKError: Error { case loadHLSStreamError } +// Define reason codes returned by Playback SDK +public enum PlaybackErrorReason: Equatable { + // Http error 400 + case headerError + case badRequestError + case siteNotFound + case configurationError + case apiKeyError + case mpPartnerError + + // Http error 401 + case tokenError + case tooManyDevices + case tooManyRequests + case noEntitlement + case noSubscription + case noActiveSession + case notAuthenticated + + // Http error 404 + case noEntityExist + + // Unknown error with associated custom message + case unknownError(String) + + init(fromString value: String) { + switch value.uppercased() { + case "HEADER_ERROR": self = .headerError + case "BAD_REQUEST_ERROR": self = .badRequestError + case "SITE_NOT_FOUND": self = .siteNotFound + case "CONFIGURATION_ERROR": self = .configurationError + case "API_KEY_ERROR": self = .apiKeyError + case "MP_PARTNER_ERROR": self = .mpPartnerError + case "TOKEN_ERROR": self = .tokenError + case "TOO_MANY_DEVICES": self = .tooManyDevices + case "TOO_MANY_REQUESTS": self = .tooManyRequests + case "NO_ENTITLEMENT": self = .noEntitlement + case "NO_SUBSCRIPTION": self = .noSubscription + case "NO_ACTIVE_SESSION": self = .noActiveSession + case "NOT_AUTHENTICATED": self = .notAuthenticated + case "NO_ENTITY_EXIST": self = .noEntityExist + default: self = .unknownError(value) + } + } +} + + /** Define the errors that can occur during API interactions */ @@ -30,7 +77,7 @@ public enum PlayBackAPIError: Error { case loadHLSStreamError case networkError(Error) - case apiError(statusCode: Int, message: String, reason: String) + case apiError(statusCode: Int, message: String, reason: PlaybackErrorReason) }