0
votes

I need to handle the api error codes like this and throw error for some status code. But follow code shows the above error. how can i achieve this?

func login(data: [String: Any], completion: @escaping (ResponseModel<SignUpModel>?) -> Void) throws {
        NetworkAdapter.request(target: .login(data: data), success: { (response) in

            if let responseModel =  try? JSONDecoder().decode(ResponseModel<SignUpModel>.self,from: response.data) {
                switch responseModel.statusCode {
                case 2000:
                    completion(responseModel)
                case 4005:
                    throw ValidationError.athenticationFailure
                case .none,.some:
                    break
                }
                completion(responseModel)
            } else {
            }
        })
    }
1

1 Answers

1
votes

You cannot

throw ValidationError.athenticationFailure

because the request is asynchronous. What you can do is to change the completion type to Result<ResponseModel<SignUpModel>, ValidationError> to return

completion(.success(responseModel))

on success and

completion(.failure(athenticationFailure)

on failure. By the way I buy an u 😉