-
Notifications
You must be signed in to change notification settings - Fork 0
/
WPError.m
90 lines (78 loc) · 4.78 KB
/
WPError.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// WPError.m
// WordPress
//
// Created by Jorge Bernal on 4/17/12.
// Copyright (c) 2012 WordPress. All rights reserved.
//
#import "WPError.h"
#import "AppDelegate.h"
NSString * const WPErrorResponseKey = @"wp_error_response";
@implementation WPError
+ (NSError *)errorWithResponse:(NSHTTPURLResponse *)response error:(NSError *)error {
NSMutableDictionary *userInfo = [[error.userInfo mutableCopy] autorelease];
[userInfo setValue:response forKey:WPErrorResponseKey];
return [NSError errorWithDomain:error.domain code:error.code userInfo:[NSDictionary dictionaryWithDictionary:userInfo]];
}
+ (void)showAlertWithError:(NSError *)error title:(NSString *)title {
NSString *message = nil;
NSString *customTitle = nil;
NSHTTPURLResponse *response = (NSHTTPURLResponse *)[error.userInfo objectForKey:WPErrorResponseKey];
if ([error.domain isEqual:AFNetworkingErrorDomain]) {
switch (error.code) {
case NSURLErrorBadServerResponse:
if (response) {
switch (response.statusCode) {
case 400:
case 405:
case 406:
case 411:
case 412:
case 413:
case 414:
case 415:
case 416:
case 417:
customTitle = NSLocalizedString(@"Incompatible site", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error being returned from the site.");
message = [NSString stringWithFormat:NSLocalizedString(@"Your WordPress site returned a error %d.\nThat probably means you have some special configuration that is not compatible with this app.\nPlease let us know in the forums about it.", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error being returned from the site."), response.statusCode];
break;
case 403:
customTitle = NSLocalizedString(@"Forbidden Access", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error accessing the site.");
message = NSLocalizedString(@"Received 'Forbidden Access'.\nIt seems there is a problem with your WordPress site", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error accessing the site.");
break;
case 500:
case 501:
customTitle = NSLocalizedString(@"Internal Server Error", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error accessing the site, most likely due to an error with the server hosting the WP install.");
message = NSLocalizedString(@"Received 'Internal Server Error'.\nIt seems there is a problem with your WordPress site", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error accessing the site, most likely due to an error with the server hosting the WP install.");
break;
case 502:
case 503:
case 504:
customTitle = NSLocalizedString(@"Temporary Server Error", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error accessing the site, most likely due to an error with the server hosting the WP install, but may be a temporary issue.");
message = NSLocalizedString(@"It seems your WordPress site is not accessible at this time, please try again later", @"Error message shown in the set up process if the WP install was unable to be added to the app due to an error accessing the site, most likely due to an error with the server hosting the WP install, but may be a temporary issue.");
break;
default:
break;
}
}
break;
default:
break;
}
}
if (message == nil) {
message = [error localizedDescription];
}
if (title == nil) {
if (customTitle == nil) {
title = @"error";
} else {
title = customTitle;
}
}
[[AppDelegate sharedWordPressApp] showAlertWithTitle:title message:message];
}
+ (void)showAlertWithError:(NSError *)error {
[self showAlertWithError:error title:nil];
}
@end