0

我正在使用 Restkit .20 并试图获取在失败块中返回的错误对象。我在下面都尝试过,并且都为错误对象返回(null)。

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:
 [RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
                                                                                pathPattern:nil
                                                                                    keyPath:@"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[self.objectManager addResponseDescriptorsFromArray:@[errorDescriptor]];

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]];
[self.objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)]];

这是在我的失败块中打印出“errorMessage:(null)”

 failure:^(RKObjectRequestOperation *operation, NSError *error) 
{
        NSLog(@"errorMessage: %@", [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey]);
}

这是从服务器返回的 JSON 中的内容。

{
    "status": "FAIL",
    "user_errors": null,
    "error": "Credit card type is not accepted by this merchant account.",
    "user": null
}
4

1 回答 1

1

HTTP 状态代码 201 是成功代码,而不是错误代码,因此不会尝试您的错误映射(因为statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError))。

因此,更新服务器以发送适当的状态代码。如果您更新响应描述符以检查错误的“成功”响应,我认为它会起作用。

于 2013-11-02T08:53:52.097 回答