我需要处理这样的api错误代码,并为某些状态代码抛出错误。但是遵循代码显示了以上错误。我怎样才能做到这一点?

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 {
            }
        })
    }

最佳答案

你不能

throw ValidationError.athenticationFailure

因为请求是异步的。您可以将完成类型更改为Result<ResponseModel<SignUpModel>, ValidationError>以返回
completion(.success(responseModel))

成功与
completion(.failure(athenticationFailure)

失败。顺便说一句,我买了u😉

10-08 06:38