UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Handle errors in HTTP request with RxSwift

Forums > Swift

Hi, I want to remove FatalErrors from my code, I tried in different ways, but I think I'm doing it wrong. This is my old code:

import Foundation
import UIKit
import RxSwift
import RxCocoa

class Service {
    func graphQL(body: [String: Any?]) -> Observable<Foundation.Data> {
        guard let urlValue = Bundle.main.urlValue else { fatalError("Error with info.plist") }
        let request = urlRequest(urlValue: urlValue, body: body)
        return URLSession.shared.rx.data(request: request)
    }

    private func urlRequest(urlValue: String, body: [String: Any?]) -> URLRequest {
        guard let url = URL(string: urlValue) else { fatalError("Error with urlValue") }
        var request = URLRequest(url: url)
        let userKey = Bundle.main.userKeyValue
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue(userKey, forHTTPHeaderField: "userid")
        request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
        return request
    }
}

Now, Im trying to do something like this:

import Foundation
import UIKit
import RxSwift
import RxCocoa

class Service {
    func graphQL(body: [String: Any?]) -> Observable<Foundation.Data> {
        guard let urlValue = Bundle.main.urlValue else { return nil }
        guard let request = urlRequest(urlValue: urlValue, body: body) else { return nil }
        return URLSession.shared.rx.data(request: request)
    }

    private func urlRequest(urlValue: String, body: [String: Any?]) -> URLRequest? {
        guard let url = URL(string: urlValue) else { return nil }
        var request = URLRequest(url: url)
        let userKey = Bundle.main.userKeyValue
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue(userKey, forHTTPHeaderField: "userid")
        request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
        return request
    }
}

but I get errors: 'nil' is incompatible with return type 'Observable <Data>', and if I add to Observable <Foundation.Data>? as optional, then I get errors in my serviceManager.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.