Skip to content

Commit b9b6fd2

Browse files
committed
Keeping response standard between AFSuccess and AFFailure
1 parent 656531c commit b9b6fd2

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/https.ios.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,48 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
5959
}
6060

6161
function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError) {
62-
let data: NSData = error.userInfo.valueForKey(AFNetworkingOperationFailingURLResponseDataErrorKey);
63-
let body = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding).toString();
64-
65-
try {
66-
body = JSON.parse(body);
67-
} catch (ignore) {
68-
}
69-
70-
let content: any = {
71-
body,
72-
description: error.description,
73-
reason: error.localizedDescription,
74-
url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
75-
};
76-
77-
if (policies.secured === true) {
78-
content.description = 'nativescript-https > Invalid SSL certificate! ' + content.description;
79-
}
62+
let data: NSDictionary<string, any> & NSData & NSArray<any> = error.userInfo.valueForKey(AFNetworkingOperationFailingURLResponseDataErrorKey);
63+
let content: any;
64+
if (data && data.class) {
65+
if (data.enumerateKeysAndObjectsUsingBlock || (<any>data) instanceof NSArray) {
66+
let serial = NSJSONSerialization.dataWithJSONObjectOptionsError(data, NSJSONWritingOptions.PrettyPrinted);
67+
content = NSString.alloc().initWithDataEncoding(serial, NSUTF8StringEncoding).toString();
68+
} else if ((<any>data) instanceof NSData) {
69+
content = NSString.alloc().initWithDataEncoding(data, NSASCIIStringEncoding).toString();
70+
} else {
71+
content = data;
72+
}
8073

81-
let reason = error.localizedDescription;
82-
resolve({task, content, reason});
74+
try {
75+
content = JSON.parse(content);
76+
} catch (e) {
77+
}
78+
} else {
79+
content = data;
80+
}
81+
/*let body = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding).toString();
82+
try {
83+
body = JSON.parse(body);
84+
} catch (e) {
85+
}
86+
let content: any = {
87+
body,
88+
description: error.description,
89+
reason: error.localizedDescription,
90+
url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
91+
};
92+
*/
93+
let failure: any = {
94+
body: content,
95+
description: error.description,
96+
reason: error.localizedDescription,
97+
url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
98+
};
99+
if (policies.secured === true) {
100+
failure.description = 'nativescript-https > Invalid SSL certificate! ' + error.description;
101+
}
102+
let reason = error.localizedDescription;
103+
resolve({task, content, reason, failure: failure});
83104
}
84105

85106
export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsResponse> {

0 commit comments

Comments
 (0)