I am writing a closure with error handling, but I am getting a warning "'catch' block is unreachable because no errors are thrown in 'do' block" in the catch line. This is my code:
class Service {
func graphQL(body: [String:Any], onSuccess: @escaping (Foundation.Data) -> (), onFailure: @escaping (Error) -> ()) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
onFailure(error)
}
if let data = data {
do{
onSuccess(data)
}
catch{
onFailure(error)
}
}
}.resume()
}
class TimeDepositManager {
func getTimeDeposits(onSuccess: @escaping ([TimeDeposits]) -> (), onFailure: @escaping (Error) -> ()) {
let body = ["query": "{ account { timeDeposits { returnAmount interestRate dueDate originalAmount id startDate currency } } }"
]
Service().graphQL(body: body, onSuccess: { data in
let json = try? JSONDecoder().decode(GraphQLResponse.self, from: data)
onSuccess(json?.data?.account?.timeDeposits ?? [])
}, onFailure: { error in
print(error)
})
}
I want the logic of onSuccess in the func getTimeDeposits(), but how remove this catch block warning?