Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to Propagate Errors when using Custom Boxing / Call #53

Open
lazarev opened this issue Nov 13, 2015 · 3 comments
Open

Ability to Propagate Errors when using Custom Boxing / Call #53

lazarev opened this issue Nov 13, 2015 · 3 comments

Comments

@lazarev
Copy link

lazarev commented Nov 13, 2015

Lets say I have a JSON object like this:

  { 
       success: false,
       error: {code: 1, message: "Something was wrong!"},
       data: nil
  }

And I wan't to map object like this:

@interface Response : NSObject
@property (assign, nonatomic) BOOL    isSuccess;
@property (strong, nonatomic) id      data;
@property (strong, nonatomic) NSError *error;
@end

I can map error with KZCall or by custom boxing. But this methods unable to specify any errors during certain class instantiation or mapping. Is there any way to fix it with current library implementation?

@krzysztofzablocki
Copy link
Owner

I'm unsure what you mean, mapping this response to the object is trivial, and you have a expandable system of validations and you can specify your own so they can fail if an object does not fullfill your requirements.

Can you elaborate more?

@lazarev
Copy link
Author

lazarev commented Nov 13, 2015

Looks like It wasn't very good example to explain.

Lets say we have data models described below:

User

@interface User : NSObject
@property (assign, nonatomic) NSString* name;
@property (strong, nonatomic) NSString* id;
@end

NSDictionary* userMapping = @{
@"name": KZProperty(name).isRequired(),
@"id": KZProperty(id).isRequired()
}

Server Response

@interface AuthenticationResponse : NSObject
@property (assign, nonatomic) User* user;
@property (strong, nonatomic) NSString* session;
@end

NSDictionary* responseMapping = @{
@"user": KZCall(userFromObject:, user),
@"session": KZProperty(session)
}

- (id)userFromObject:(NSDictionary *)payload
{
    User* user = [[User alloc] init];
    NSArray* errors;
    if ([KZPropertyMapper mapValuesFrom:payload toInstance:user usingMapping: userMapping  errors:&errors]) {
        return user;
    } else {
        // How to notify AuthenticationResponse mapping caller about problems in user mapping?
    }
}

Now when I want to map some dictionary on server response, I want this process to be finished with error if error appeared during user property unboxing:

NSArray* errors;
AuthenticationResponse* response;
[KZPropertyMapper mapValuesFrom: payload 
                                          toInstance: response 
                                    usingMapping: responseMapping
                                                 errors: &errors]

Here I want to receive errors if something wrong with User object. How to solve this problem?

@krzysztofzablocki krzysztofzablocki changed the title Mapping errors population during inline custom classes mapping Ability to Propagate Errors when using Custom Boxing / Call Nov 16, 2015
@krzysztofzablocki
Copy link
Owner

I get it now, you want to have ability to propagate errors up the chain, so we could add functionality to the mapper that when returning an NSError from a boxing/call it should treat that as failure and fail the whole mapping.

@lazarev Would you like to try implementing that? should be straighforward, if not I'll try to find some time this weekend / next week to do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants