Emit RxError.unknown inside map array

Hello,

Reading the book, page 169 (Intermission: Handling erroneous input), I see that you use array flatmap to discard data models that cannot be mapped properly, returning nil with a failable init, and flatmap filters them.

However, my question is, what about if I want trigger Observable.error(RxError.unknown) when data mapping fails?

Right now, this is my code but RxError.unknown does not work, I guess because it is a normal map (map from Data to array of elements).

This is my code:

func fetchApplianceList() -> Observable<[ApplianceMatrixData]> {
    guard let url = URL(string: RequestURLs.applianceMatrixURL) else { return Observable.error(URLError(.badURL)) }
    
    //     func getSSL(url: URL) -> Observable<Data>

    return manager.getSSL(url: url).map { data in
        var applianceArray = [ApplianceMatrixData]()
        do {
            applianceArray = try JSONDecoder().decode([ApplianceMatrixData].self, from: data)
        } catch {
            DDLogError("Error: \(error.localizedDescription)")
            //return Observable.error(RxError.unknown)
            //return Observable.error(URLError(.badURL))
        }

        // loop through appliance matrix list, only add appliances where an image is available (required in case API ever gets updated and new appliances are added which the app is not ready for)
        var availableAppliances = [ApplianceMatrixData]()
        for appliance in applianceArray {
            // prevent collection appliances from being added to list
            if appliance.isCollection || !appliance.enabled { continue }

            let imageName = UtilityMethods.fetchImageById(iconId: appliance.imageID)

            // make sure image exists
            if UIImage(named: "\(imageName)") != nil {
                availableAppliances.append(appliance)
            }
        }
        return availableAppliances
    }
}

How can I trigger Observable.error(RxError.unknown) when mapping data?

Thanks a lot.

@ricardo1980 Do you still have issues with this?

This topic was automatically closed after 166 days. New replies are no longer allowed.