Skip to content

Commit 3b917db

Browse files
committed
Adding onError to Request.JSON for invalid JSON
1 parent 2433c48 commit 3b917db

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Docs/Request/Request.JSON.md

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ Fired when the request completes. This overrides the signature of the Request su
3434
1. responseJSON - (*object*) The JSON response object from the remote request.
3535
2. responseText - (*string*) The JSON response as string.
3636

37+
#### error
38+
39+
Fired when the parsed JSON is not valid and the secure option is set.
40+
41+
##### Signature:
42+
43+
onError(text, error)
44+
45+
##### Arguments:
46+
47+
1. text - (string) The response text.
48+
2. error - (string) The error message.
49+
3750
### Returns:
3851

3952
* (*object*) A new Request.JSON instance.

Source/Request/Request.JSON.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Request.JSON = new Class({
1919
Extends: Request,
2020

2121
options: {
22+
/*onError: function(text, error){},*/
2223
secure: true
2324
},
2425

@@ -31,11 +32,13 @@ Request.JSON = new Class({
3132
},
3233

3334
success: function(text){
34-
var secure = this.options.secure;
35-
var json = this.response.json = Function.attempt(function(){
36-
return JSON.decode(text, secure);
37-
});
38-
35+
var json;
36+
try {
37+
json = this.response.json = JSON.decode(text, this.options.secure);
38+
} catch (error){
39+
this.fireEvent('error', [text, error]);
40+
return;
41+
}
3942
if (json == null) this.onFailure();
4043
else this.onSuccess(json, text);
4144
}

0 commit comments

Comments
 (0)