Home > Back-end >  How to print error message while parsing in swift
How to print error message while parsing in swift

Time:01-30

this is json error structure:

{
    "jsonrpc": "2.0",
    "error": {
        "code": "-32700",
        "message": "Parse error",
        "meaning": "Could not decode token: Error while decoding to JSON: Syntax error"
    }
}

this is success response structure:

{
    "jsonrpc": "2.0",
    "result": {
        "users": [
            {
                "id": 371,..... so on data

here am unable to print error > message in my code

code: here able to print json response result or error dictionary in case .success(_) print statement but how to print only error > meaning

how to print error's "Parse error" in below code, please guide me

class GeneralResponse<T: Codable>: Codable {
    var result: T?
    let error: ErrorClass?
}

struct ErrorClass: Codable {
    let code, message, meaning: String?
}

struct RequestObject {
    var params: [String: AnyObject]? = nil
    var method: HTTPMethod
    var path: String
    var isTokenNeed: Bool = false
    var vc: UIViewController?
}

class NetworkManager {
    
    private let decoder: JSONDecoder
    static let sharedInstance = NetworkManager()
    
    public init(_ decoder: JSONDecoder = JSONDecoder()) {
        self.decoder = decoder
    }
    
    public func serviceCall<T: Codable>(_ objectType: T.Type,
                                        with request: RequestObject,
                                        completion: @escaping  (T?, Error?) -> Void)  {
        
        
        let paramsDict  = ["jsonrpc" : "2.0", "params" : request.params ?? nil] as [String : Any?]
        
        
        AF.request(request.path, method: request.method, parameters: paramsDict as [String : AnyObject], encoding: JSONEncoding.default, headers: "Accept": "application/json")
            .responseJSON { response in
                
                switch response.result {
                case .success(_):
                    do {
                        print("only json respopnse \(response)")
                        
                        let data = response.data
                        let responseData  = try self.decoder.decode(T.self, from: data ?? Data())
                        completion(responseData, nil)
                    } catch {
                        completion(nil, error)
                        print("in catch \(error)")
                    }
                case .failure(let AFError):
                    let error = AFError
                    print(error.localizedDescription)
                    print("failure error: \(error)")
                }
            }
    }
}

o/p for error in above call with print("only json respopnse \(response)")

only json respopnse success({
error =     {
    code = "-32700";
    meaning = "Token Signature could not be verified.";
    message = "Parse error";
};
jsonrpc = "2.0";
})

EDIT 2: here success response not coming in viewcontroller

struct PostModel: Codable {
    let jsonrpc: String?
    let result: PostResult?
}
struct PostResult: Codable {
    let users: [PostUser]?
}
struct PostUser: Codable {
    let id: Int?
}

and calling like this in vc

var k12Data: PostModel?

let paramet = ["location": "", "country": "", "gender": "", "keyword": ""] as [String : AnyObject]

let request = RequestObject(params: paramet, method: .post, path: "https://phpwebdevelopmentserv/", isTokenNeed: true, vc: self)
NetworkManager.sharedInstance.serviceCall(PostModel.self, with: request) { (response, error) in
print("viewcontroller data \(response)")
}

o/p

viewcontroller data Optional(TestigAllInOne.PostModel(jsonrpc: nil, result: nil))

CodePudding user response:

You didn´t show us your Postmodel so I am asuming it is of some type of GeneralResponse<T> class.

You should decode to your GeneralResponse class:

let responseData = try self.decoder.decode(GeneralResponse<T>.self, from: data)

now you can print your error if one exists:

print(responseData.error?.meaning ?? "no error")

and call your completion handler like this:

guard let result = responseData.result else{
    // unknown error has occured
    completion(nil, nil) // you probably need to create a custom error here
    return
}

completion(result, nil)

the main essence is to not pass GeneralResponse as argument to your function but the result type you are expecting. So if for example your User type looks like this:

struct User: Codable{
    var id: String
}

call it like this:

networkmanager.serviceCall([User].self, with: RequestObject(...)) { users, error in
    // users is of type `[User]` here.
}
  • Related