fix($http): throw more informative error on invalid JSON response
Fixes #15695 Closes #15724
This commit is contained in:
committed by
Georgios Kalpakas
parent
11d9ad1eb2
commit
c80fa1cfe1
@@ -0,0 +1,14 @@
|
||||
@ngdoc error
|
||||
@name $http:baddata
|
||||
@fullName Bad JSON Data
|
||||
@description
|
||||
|
||||
The default @{link ng.$http#default-transformations `transformResponse`} will try to parse the
|
||||
response as JSON if the `Content-Type` header is `application/json` or the response looks like a
|
||||
valid JSON-stringified object or array.
|
||||
This error occurs when that data is not a valid JSON object.
|
||||
|
||||
The error message should provide additional context such as the actual response.
|
||||
|
||||
To resolve this error, make sure you pass valid JSON data to `transformResponse` or use an
|
||||
appropriate `Content-Type` header for non-JSON data.
|
||||
+6
-1
@@ -138,7 +138,12 @@ function defaultHttpResponseTransform(data, headers) {
|
||||
if (tempData) {
|
||||
var contentType = headers('Content-Type');
|
||||
if ((contentType && (contentType.indexOf(APPLICATION_JSON) === 0)) || isJsonLike(tempData)) {
|
||||
data = fromJson(tempData);
|
||||
try {
|
||||
data = fromJson(tempData);
|
||||
} catch (e) {
|
||||
throw $httpMinErr('baddata', 'Data must be a valid JSON object. Received: "{0}". ' +
|
||||
'Parse error: "{1}"', data, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-6
@@ -1369,17 +1369,15 @@ describe('$http', function() {
|
||||
}
|
||||
);
|
||||
|
||||
it('should forward json deserialization errors to the http error handler',
|
||||
function() {
|
||||
it('should return JSON data with error message if JSON is invalid', function() {
|
||||
var errCallback = jasmine.createSpy('error');
|
||||
|
||||
$httpBackend.expect('GET', '/url').respond('abcd', {'Content-Type': 'application/json'});
|
||||
$http({method: 'GET', url: '/url'}).then(callback).catch(errCallback);
|
||||
$httpBackend.expect('GET', '/url').respond('{abcd}', {'Content-Type': 'application/json'});
|
||||
$http.get('/url').then(callback).catch(errCallback);
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
expect(errCallback).toHaveBeenCalledOnce();
|
||||
expect(errCallback.calls.mostRecent().args[0]).toEqual(jasmine.any(SyntaxError));
|
||||
expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr('$http', 'baddata');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user