Commit 50f26233 by Brent Gulanowski Committed by Gabriel O'Flaherty-Chan

Make checkout error mapping more robust.

parent bde22786
...@@ -25,26 +25,49 @@ ...@@ -25,26 +25,49 @@
// //
#import "BUYError+BUYAdditions.h" #import "BUYError+BUYAdditions.h"
#import "NSArray+BUYAdditions.h"
@interface NSDictionary (BUYCheckoutErrorParsing)
- (NSDictionary *)buy_errorsDictionary;
- (NSDictionary *)buy_checkoutErrorsDictionary;
- (NSArray *)buy_lineItemErrors;
@end
@implementation NSDictionary (BUYCheckoutErrorParsing)
- (NSDictionary *)buy_errorsDictionary
{
NSDictionary *errorsDictionary = self[@"errors"];
return [errorsDictionary isKindOfClass:[NSDictionary class]] ? errorsDictionary : nil;
}
- (NSDictionary *)buy_checkoutErrorsDictionary
{
NSDictionary *checkoutErrorsDictionary = [self buy_errorsDictionary][@"checkout"];
return [checkoutErrorsDictionary isKindOfClass:[NSDictionary class]] ? checkoutErrorsDictionary : nil;
}
- (NSArray *)buy_lineItemErrors
{
NSArray *lineItemErrors = [self buy_checkoutErrorsDictionary][@"line_items"];
return [lineItemErrors isKindOfClass:[NSArray class]] ? lineItemErrors : nil;
}
@end
@implementation BUYError (Checkout) @implementation BUYError (Checkout)
+ (NSArray<BUYError *> *)errorsFromCheckoutJSON:(NSDictionary *)json + (NSArray<BUYError *> *)errorsFromCheckoutJSON:(NSDictionary *)json
{ {
NSArray *lineItems = json[@"errors"][@"checkout"][@"line_items"]; return [[json buy_lineItemErrors] buy_map:^id(NSDictionary<NSString *, NSArray *> *lineItemError) {
NSMutableArray *errors = [NSMutableArray array]; return [lineItemError isKindOfClass:[NSDictionary class]] ? [self errorWithJSONDictionary:lineItemError] : [NSNull null];
}];
for (NSDictionary<NSString *, NSArray *> *lineItem in lineItems) { }
if (lineItem == (id)[NSNull null]) {
[errors addObject:lineItem]; + (BUYError *)errorWithJSONDictionary:(NSDictionary<NSString *, NSArray *> *)dictionary
} {
else { NSString *key = dictionary.allKeys.firstObject;
for (NSString *key in lineItem.allKeys) { return [[self alloc] initWithKey:key json:dictionary[key].firstObject];
NSDictionary *reason = [lineItem[key] firstObject];
[errors addObject:[[BUYError alloc] initWithKey:key json:reason]];
};
}
};
return errors;
} }
- (NSString *)quantityRemainingMessage - (NSString *)quantityRemainingMessage
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment