Permalink
Browse files

Adding onError to Request.JSON for invalid JSON

  • Loading branch information...
1 parent 2433c48 commit 3b917db3f3681f4b57132ef9872b31073cdca6a3 @cpojer cpojer committed Feb 15, 2011
Showing with 22 additions and 6 deletions.
  1. +13 −0 Docs/Request/Request.JSON.md
  2. +8 −5 Source/Request/Request.JSON.js
  3. +1 −1 Specs
@@ -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.
@@ -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);
}

0 comments on commit 3b917db

Please sign in to comment.