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 1 commit
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
13 changes: 13 additions & 0 deletions docs/content/error/$http/baddata.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@ngdoc error
@name $http:baddata
@fullName Bad JSON Data
@description

This error occurs when the data parameter passed to the {@link ng.$http `defaultHttpResponseTransform`} service is not a valid JSON object.
Copy link
Member

Choose a reason for hiding this comment

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

Please limit lines to 100 chars.

Copy link
Member

Choose a reason for hiding this comment

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

The link should point tot a specific section (e.g. ng.$httpProvider.defaults, ng.$http.defaults, or ng$http#default-transformations).

BTW, I just noticed that $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse are not documented. They should be.

`defaultHttpResponseTransform` expects the first parameter as a valid JSON object if the second parameter of headers specifies a Content-Type of JSON.
Copy link
Member

Choose a reason for hiding this comment

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

Don't mention defaultHttpResponseTransform, because it is private and the users don't know what it is.
Just say that "The default 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".


The error message should provide additional context such as the actual value of the parameter that was received.
Copy link
Member

Choose a reason for hiding this comment

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

actual value of the parameter that was received --> actual response


To resolve this error, make sure you pass a valid JSON data object to `defaultHttpResponseTransform`.

For more information, see the {@link ng.$http `defaultHttpResponseTransform`} service API documentation.
6 changes: 5 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ 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 minErr('$http')('baddata', 'Data must be a valid JSON object. Received: {0}', data);
Copy link
Member

Choose a reason for hiding this comment

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

We already have an $httpMinErr instance. Use that.

Copy link
Member

Choose a reason for hiding this comment

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

Also, please, embed the actual error message into the minErr.

}
}
}
}
Expand Down