Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($http): JSON parse failure #15724

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/content/error/$http/baddata.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@ngdoc error
@name $http:baddata
@fullName Bad JSON Data
@description

The default @{link ng$http#default-transformations `transformResponse`} will try to parse the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be ng.$http#... (with a dot after ng)?

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 a valid JSON data object to `transformResponse`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a valid JSON data object --> valid JSON data

Also, the correct resolution depends on the source of the problem. In some cases, one might need to send correct headers (to not include application/json for example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

To resolve this error, make sure you pass valid JSON data to transformResponse or use the appropriate Content-Type instead of the specified application/json if passing non-JSON data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

To resolve this error, make sure you pass valid JSON data to transformResponse or use an appropriate Content-Type header for non-JSON data.


For more information, see the {@link ng$http#default-transformations `transformResponse`} service
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, since we already link to default-transformations above imo.

API documentation.
7 changes: 6 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}". ' +
'Error occurred: "{1}"', data, e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe: Error occurred --> Original error or Parse error

}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,17 @@ describe('$http', function() {
expect(callback.calls.argsFor(1)[0]).toBe(null);
expect(callback.calls.argsFor(2)[0]).toBe('');
});

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong indentation.

Nit: It is simpler to use $http.get('/url').

$httpBackend.flush();

expect(callback).not.toHaveBeenCalled();
expect(errCallback).toHaveBeenCalledOnce();
expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr('$http', 'baddata');
});
});
});

Expand Down