forked from afh/trundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSError_CouchDBExtensions.m
73 lines (57 loc) · 2.29 KB
/
NSError_CouchDBExtensions.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
//
// NSError_CouchDBExtensions.m
// CLI Sample
//
// Created by Jonathan Wight on 05/26/10.
// Copyright 2010 toxicsoftware.com. All rights reserved.
//
#import "NSError_CouchDBExtensions.h"
#import "CouchDBClientConstants.h"
@implementation NSError (NSError_CouchDBExtensions)
+ (NSError *)couchDBErrorWithError:(NSError *)inError JSONDictionary:(NSDictionary *)inJSONDictionary
{
NSError *theError = NULL;
if ([inError.domain isEqual:NSURLErrorDomain] && (inError.code < 200 || inError.code >= 300))
{
NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionary];
[theUserInfo setObject:inError forKey:NSUnderlyingErrorKey];
if ([inJSONDictionary objectForKey:@"reason"] != NULL)
[theUserInfo setObject:[inJSONDictionary objectForKey:@"reason"] forKey:NSLocalizedDescriptionKey];
if (inJSONDictionary)
[theUserInfo setObject:inJSONDictionary forKey:@"json"];
NSInteger theErrorCode = [self errorCodeForCouchDBError:[inJSONDictionary objectForKey:@"reason"]];
theError = [NSError errorWithDomain:kCouchErrorDomain code:theErrorCode userInfo:theUserInfo];
}
return(theError);
}
+ (NSError *)couchDBErrorWithURLResponse:(NSURLResponse *)inURLResponse JSONDictionary:(NSDictionary *)inJSONDictionary
{
NSError *theError = NULL;
NSHTTPURLResponse *theHTTPResponse = (NSHTTPURLResponse *)inURLResponse;
NSInteger theStatusCode = theHTTPResponse.statusCode;
if (inJSONDictionary == NULL || theStatusCode < 200 || theStatusCode >= 300)
{
NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionary];
if (theHTTPResponse)
[theUserInfo setObject:theHTTPResponse forKey:@"Response"];
if ([inJSONDictionary objectForKey:@"reason"] != NULL)
[theUserInfo setObject:[inJSONDictionary objectForKey:@"reason"] forKey:NSLocalizedDescriptionKey];
if (inJSONDictionary)
[theUserInfo setObject:inJSONDictionary forKey:@"json"];
NSInteger theErrorCode = [self errorCodeForCouchDBError:[inJSONDictionary objectForKey:@"reason"]];
theError = [NSError errorWithDomain:kCouchErrorDomain code:theErrorCode userInfo:theUserInfo];
}
return(theError);
}
+ (NSInteger)errorCodeForCouchDBError:(NSString *)inError
{
if ([inError isEqualToString:@"no_db_file"])
{
return(CouchDBErrorCode_NoDatabase);
}
else
{
return(CouchDBErrorCode_ServerError);
}
}
@end