Skip to content

Commit 573e0da

Browse files
committed
fix($http): JSON parse failure
Fixes angular#15695
1 parent 68ced16 commit 573e0da

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

docs/content/error/$http/baddata.ngdoc

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
@fullName Bad JSON Data
44
@description
55

6-
This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object.
7-
`defaultHttpResponseTransform` expects the first parameter as a valid JSON object if the second parameter of headers specifies a Content-Type of JSON.
6+
The default @{link ng$http#default-transformations `transformResponse`} will try to parse the
7+
response as JSON if the `Content-Type` header is `application/json` or the response looks like a
8+
valid JSON-stringified object or array.
9+
This error occurs when that data is not a valid JSON object.
810

9-
The error message should provide additional context such as the actual value of the parameter that was received.
11+
The error message should provide additional context such as the actual response.
1012

11-
To resolve this error, make sure you pass a valid JSON data object to `defaultHttpResponseTransform`.
13+
To resolve this error, make sure you pass a valid JSON data object to `transformResponse`.
1214

13-
For more information, see the {@link ng.$http `defaultHttpResponseTransform`} service API documentation.
15+
For more information, see the {@link ng$http#default-transformations `transformResponse`} service
16+
API documentation.

src/ng/http.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ function defaultHttpResponseTransform(data, headers) {
141141
try {
142142
data = fromJson(tempData);
143143
} catch (e) {
144-
throw minErr('$http')('baddata', 'Data must be a valid JSON object. Received: {0}', data);
144+
throw $httpMinErr('baddata', 'Data must be a valid JSON object. Received: "{0}". ' +
145+
'Error occurred: "{1}"', data, e);
145146
}
146147
}
147148
}

test/ng/httpSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,17 @@ describe('$http', function() {
14421442
expect(callback.calls.argsFor(1)[0]).toBe(null);
14431443
expect(callback.calls.argsFor(2)[0]).toBe('');
14441444
});
1445+
1446+
it('should return JSON data with error message if JSON is invalid', function() {
1447+
var errCallback = jasmine.createSpy('error');
1448+
$httpBackend.expect('GET', '/url').respond('{abcd}', {'Content-Type': 'application/json'});
1449+
$http({method: 'GET', url: '/url'}).then(callback).catch(errCallback);
1450+
$httpBackend.flush();
1451+
1452+
expect(callback).not.toHaveBeenCalled();
1453+
expect(errCallback).toHaveBeenCalledOnce();
1454+
expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr('$http', 'baddata');
1455+
});
14451456
});
14461457
});
14471458

0 commit comments

Comments
 (0)