Restkit json error response msg from server

后端 未结 4 740
暖寄归人
暖寄归人 2020-12-28 18:50

I have used many hours on how to solve this issue. Im using Restkit 0.9.3 with Object Mapping 2.0. All data is in JSON. I can make GET, POST, PUT and DELETE operations corre

4条回答
  •  时光说笑
    2020-12-28 19:18

    For version 0.10.0 this response error can be mapped as follows:

    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
    [errorMapping mapKeyPath:@"message" toAttribute:@"errorMessage"];
    [[[RKObjectManager sharedManager] mappingProvider] setErrorMapping:errorMapping];
    

    Requests that return an error will call the following delegate method:

    - (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
        NSArray *errorMessages = [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey];
        RKErrorMessage *errorMessage = [errorMessages objectAtIndex:0]; // First and only object in your case.
        NSString *message = [errorMessage errorMessage];
        NSInteger code = [[objectLoader response] statusCode];
        NSLog(@"ERROR: [%d] %@", code, message); // => ERROR: [401] Unauthorized
    }
    

提交回复
热议问题