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

Commit 529cf2f

Browse files
committed
Wrapped fromJSON call in try-catch to throw relevant error, added documentation for baddata
1 parent 71f437c commit 529cf2f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@ngdoc error
2+
@name $http:baddata
3+
@fullName Bad JSON Data
4+
@description
5+
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.
8+
9+
The error message should provide additional context such as the actual value of the parameter that was received.
10+
11+
To resolve this error, make sure you pass a valid JSON data object to `defaultHttpResponseTransform`.
12+
13+
For more information, see the {@link ng.$http `defaultHttpResponseTransform`} service API documentation.

src/ng/http.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ function defaultHttpResponseTransform(data, headers) {
138138
if (tempData) {
139139
var contentType = headers('Content-Type');
140140
if ((contentType && (contentType.indexOf(APPLICATION_JSON) === 0)) || isJsonLike(tempData)) {
141-
data = fromJson(tempData);
141+
try {
142+
data = fromJson(tempData);
143+
}
144+
catch(e) {
145+
throw minErr('$http')('baddata', 'Data must be a valid JSON object. Received: {0}', data);
146+
}
142147
}
143148
}
144149
}

0 commit comments

Comments
 (0)