Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding onError to Request.JSON for invalid JSON

  • Loading branch information...
commit 3b917db3f3681f4b57132ef9872b31073cdca6a3 1 parent 2433c48
@cpojer cpojer authored
View
13 Docs/Request/Request.JSON.md
@@ -34,6 +34,19 @@ Fired when the request completes. This overrides the signature of the Request su
1. responseJSON - (*object*) The JSON response object from the remote request.
2. responseText - (*string*) The JSON response as string.
+#### error
+
+Fired when the parsed JSON is not valid and the secure option is set.
+
+##### Signature:
+
+ onError(text, error)
+
+##### Arguments:
+
+1. text - (string) The response text.
+2. error - (string) The error message.
+
### Returns:
* (*object*) A new Request.JSON instance.
View
13 Source/Request/Request.JSON.js
@@ -19,6 +19,7 @@ Request.JSON = new Class({
Extends: Request,
options: {
+ /*onError: function(text, error){},*/
secure: true
},
@@ -31,11 +32,13 @@ Request.JSON = new Class({
},
success: function(text){
- var secure = this.options.secure;
- var json = this.response.json = Function.attempt(function(){
- return JSON.decode(text, secure);
- });
-
+ var json;
+ try {
+ json = this.response.json = JSON.decode(text, this.options.secure);
+ } catch (error){
+ this.fireEvent('error', [text, error]);
+ return;
+ }
if (json == null) this.onFailure();
else this.onSuccess(json, text);
}
2  Specs
@@ -1 +1 @@
-Subproject commit c47fa5d10719d7a4bf55c6cb1b7d7fa194548fe2
+Subproject commit 4a5b3971e3f6333124af3b1f9966721bf5b22933
Please sign in to comment.
Something went wrong with that request. Please try again.