From bfe59c7a96dc5d431f0788246502244cdf137bd8 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Thu, 7 Nov 2024 11:19:43 +0100 Subject: [PATCH] refactor: Clearer code paths --- Sources/Confidence/FlagEvaluation.swift | 35 +++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Sources/Confidence/FlagEvaluation.swift b/Sources/Confidence/FlagEvaluation.swift index c04d6c61..1b70df04 100644 --- a/Sources/Confidence/FlagEvaluation.swift +++ b/Sources/Confidence/FlagEvaluation.swift @@ -44,17 +44,12 @@ extension FlagResolution { ) } - if resolvedFlag.resolveReason == .targetingKeyError { - return Evaluation( - value: defaultValue, - variant: nil, - reason: .targetingKeyError, - errorCode: .invalidContext, - errorMessage: "Invalid targeting key" - ) + if let evaluation = checkBackendErrrs(resolvedFlag: resolvedFlag, defaultValue: defaultValue) { + return evaluation } guard let value = resolvedFlag.value else { + // No backend error, but nil value returned. This can happend with "noSegmentMatch" or "archived", for example Task { await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken) } @@ -133,6 +128,30 @@ extension FlagResolution { } // swiftlint:enable function_body_length + private func checkBackendErrrs(resolvedFlag: ResolvedValue, defaultValue: T) -> Evaluation? { + if resolvedFlag.resolveReason == .targetingKeyError { + return Evaluation( + value: defaultValue, + variant: nil, + reason: .targetingKeyError, + errorCode: .invalidContext, + errorMessage: "Invalid targeting key" + ) + } else if (resolvedFlag.resolveReason == .error + || resolvedFlag.resolveReason == .unknown + || resolvedFlag.resolveReason == .unspecified) { + return Evaluation( + value: defaultValue, + variant: nil, + reason: .error, + errorCode: .evaluationError, + errorMessage: "Unknown error from backend" + ) + } else { + return nil + } + } + // swiftlint:disable:next cyclomatic_complexity private func getTyped(value: ConfidenceValue) -> T? { if let value = self as? T {