Skip to content

Commit bc3ef26

Browse files
committed
fix: protection around json.parse
1 parent b2055ff commit bc3ef26

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/http/http.ios.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,21 @@ class NSURLSessionTaskDelegateImpl extends NSObject
283283
let responseText;
284284
if (isTextContentType(returnType)) {
285285
responseText = NSDataToString(this._data);
286-
content = NSDataToString(responseText);
286+
content = responseText;
287287
} else if (returnType.indexOf('application/json') > -1) {
288288
// @ts-ignore
289-
responseText = NSDataToString(this._data);
290-
content = JSON.parse(responseText);
291-
// content = deserialize(NSJSONSerialization.JSONObjectWithDataOptionsError(this._data, NSJSONReadingOptions.AllowFragments, null));
289+
try {
290+
responseText = NSDataToString(this._data);
291+
content = JSON.parse(responseText);
292+
// content = deserialize(NSJSONSerialization.JSONObjectWithDataOptionsError(this._data, NSJSONReadingOptions.AllowFragments, null));
293+
} catch (err) {
294+
this._reject({
295+
type: HttpError.Error,
296+
ios: null,
297+
message: err
298+
});
299+
return;
300+
}
292301
} else {
293302
content = this._data;
294303
}

src/xhr/TNSXMLHttpRequest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,11 @@ export class TNSXMLHttpRequest {
426426
if (this.responseType === XMLHttpRequestResponseType.json) {
427427
if (typeof res.content === 'string') {
428428
this._responseText = res.content;
429-
this._response = JSON.parse(this.responseText);
429+
try {
430+
this._response = JSON.parse(this.responseText);
431+
} catch (err) {
432+
// this should probably be caught before the promise resolves
433+
}
430434
} else if (typeof res.content === 'object') {
431435
this._response = res.content;
432436
this._responseText = res.responseText;

0 commit comments

Comments
 (0)