I've started to use PromiseKit in async requests and I have a problem for now, I can't parse an error in chain. I have an request manager with method like this:
func request(
url: String,
parameters: JSON? = nil,
requestMethod: HTTPMethod,
completion: @escaping completion
) {
self.logger.log(category: .network, message: "Starting request", access: .public)
Alamofire.request(
url,
method: requestMethod,
parameters: parameters,
encoding: JSONEncoding.prettyPrinted
).responseData { response in
if let data = response.data {
self.logger.log(category: .network, message: "Response: \(data)", access: .private)
completion(.success(data))
} else {
self.logger.log(category: .network, message: SWError.dataError.localizedDescription, access: .public, type: .error)
completion(.failure(SWError.dataError))
}
}
}
The request is creating like this:
public class func getSettings(
completion: @escaping (Result<Model.Settings.Settings>) -> Void
) {
RequestManager.shared.request(
url: Endpoint.Settings.slash.url(),
requestMethod: .get
) { result in
switch result {
case let .success(data):
do {
let result = try JSONDecoder().decode(Model.Settings.Settings.self, from: data)
completion(.success(result))
} catch {
Request.Shared.handleCode(from: data, completion: { serverCode in
logger.log(category: .network, message: SWError.decodingError.localizedDescription, access: .public, type: .error)
completion(.failure(serverCode))
})
}
case let .failure(error):
completion(.failure(error))
}
}
}
Promise part is here:
private func getAppSettings() -> Promise<[Model.Settings.Datum]> {
return Promise { seal in
Request.Settings.getSettings(completion: { result in
switch result {
case let .success(model):
if let data = model.data {
seal.fulfill(data)
} else {
seal.reject(SWError.create(with: "No app settings data"))
}
case let .failure(error):
seal.reject(error)
}
})
}
}
Somehow I can't catch error. What is wrong with my request? How to make it right?