Handling 401 error

This commit is contained in:
Selim Mustafaev 2023-09-02 21:22:48 +03:00
parent 2ef6968d00
commit 771b5923b6

View File

@ -46,7 +46,7 @@ public class Api {
return Single.error(self.genError("Error creating request", suggestion: ""))
}
return self.session.rx.data(request: request).asSingle().map { data in
return self.session.rx.response(request: request).asSingle().map { httpResp, data in
// let str = String(data: data, encoding: .utf8)
// print("================================")
// if let string = str?.replacingOccurrences(of: "\\\"", with: "\"")
@ -55,6 +55,15 @@ public class Api {
// print(string)
// }
// print("================================")
if httpResp.statusCode == 401 {
throw genError("Unauthorized", suggestion: "", code: httpResp.statusCode)
}
if httpResp.statusCode < 200 || httpResp.statusCode >= 300 {
throw genError("HTTP error \(httpResp.statusCode)", suggestion: "", code: httpResp.statusCode)
}
do {
let resp = try JSONDecoder().decode(Response<T>.self, from: data)
if resp.success {