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 {