Skip to content

Commit

Permalink
added stack track and optional support for javascript-stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbender committed Sep 27, 2010
1 parent 02636ea commit 9b8c7c4
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "javascript-stacktrace"]
path = javascript-stacktrace
url = http://github.com/emwendelin/javascript-stacktrace.git
2 changes: 1 addition & 1 deletion examples/app.js
@@ -1,3 +1,3 @@
jQuery.flawed(function(){
throw 'foo';
throw new TypeError('foo');
})();
1 change: 1 addition & 0 deletions examples/index.html
@@ -1,6 +1,7 @@
<html>
<head>
<script src="/lib/jquery.min.js"></script>
<script src="/javascript-stacktrace/stacktrace.js"></script>
<script src="/lib/jquery.flawed.js"></script>
<script src="app.js"></script>
</head>
Expand Down
8 changes: 5 additions & 3 deletions examples/server.rb
@@ -1,5 +1,4 @@
require 'rubygems'
require 'logger'
require 'rack'

class Rack::Flawed
Expand All @@ -9,8 +8,11 @@ def initialize(app, opts={})
end

def call(env)
if env[@header]
# Log or report exception here
req = Rack::Request.new(env)

if env[@header] && req.post?
# Log the stacktrack
p req.params
end

@app.call(env)
Expand Down
1 change: 1 addition & 0 deletions javascript-stacktrace
Submodule javascript-stacktrace added at 630c0c
18 changes: 14 additions & 4 deletions lib/jquery.flawed.js
Expand Up @@ -6,19 +6,29 @@ jQuery.flawed = function (scope){
return function(){
try {
scope.apply(this, arguments);
} catch (e) {
var request_params = jQuery.extend(jQuery.flawed.config.ajax, {
} catch (error) {
var stack, request_params;

if(!(window.printStackTrace == undefined))
stack = printStackTrace({e: error});
else
stack = error;

request_params = jQuery.extend(jQuery.flawed.config.ajax, {
data: {
url: $(location).attr('href'),
stack: e
message: error.message,
type: error.name,
stack: stack
},

beforeSend: function(request){
request.setRequestHeader(jQuery.flawed.config.header, true);
}
});

jQuery.ajax(request_params);
throw e;
throw error;
}
};
};
Expand Down
1 change: 1 addition & 0 deletions spec/dom.html
Expand Up @@ -4,6 +4,7 @@
<script src="helpers/jspec.js"></script>
<script src="helpers/jspec.xhr.js"></script>
<script src="../lib/jquery.min.js"></script>
<script src="../javascript-stacktrace/stacktrace.js"></script>
<script src="../lib/jquery.flawed.js"></script>
<script src="unit/spec.helper.js"></script>
<script>
Expand Down
71 changes: 39 additions & 32 deletions spec/unit/spec.js
@@ -1,29 +1,30 @@
describe('jQuery.flawed', function(){
before_each(function(){
JSpec.defaultContext.proxy = {};
JSpec.defaultContext.error_msg = "foo";

// set some helper methos, extent default later if default
// helpers are required
JSpec.defaultContext.expect_reraise = function(body){
JSpec.defaultContext.expect_raise = function(body){
expect(function(){

// use a default function or one provided to
// execute throw
jQuery.flawed(body || function(){
throw new TypeError(error_msg);
throw JSpec.defaultContext.proxy.error = new Error(error_msg);
})();
}).to(throw_error, TypeError, error_msg);
}).to(throw_error, Error, error_msg);
};
});

describe('error handling', function(){

it('should reraise top level exceptions', function(){
expect_reraise();
expect_raise();
});

it('should reraise exceptions from nested contexts', function(){
expect_reraise(function(){
expect_raise(function(){
jQuery(document).ready(function(){
(function(){
(function(){
Expand All @@ -39,6 +40,9 @@ describe('jQuery.flawed', function(){

before_each(function(){
JSpec.defaultContext.flawed_defaults = jQuery.flawed.config;
jQuery.ajax = function(options){
JSpec.defaultContext.proxy = options;
};
});

after_each(function(){
Expand All @@ -47,39 +51,42 @@ describe('jQuery.flawed', function(){

it('should post the path set on the flawed object', function(){
jQuery.flawed.config.ajax.path = '/foo';

expect(jQuery).to(receive, 'ajax').with_args(
jQuery.extend(jQuery.flawed.config.ajax, {
path: '/foo'
})
);

expect_reraise();
expect_raise();
expect(proxy.path).to(be, '/foo');
});

it('should use the header defined on the settings object', function(){
jQuery.flawed.config.ajax.type = 'GET';
jQuery.flawed.config.ajax.type = 'POST';
expect_raise();
expect(proxy.type).to(be, 'POST');
});

expect(jQuery).to(receive, 'ajax').with_args(
jQuery.extend(jQuery.flawed.config.ajax, {
type: 'GET'
})
);
describe('data', function(){
before_each(function(){
expect_raise();
});

expect_reraise();
});
// NOTE test for array stacktrace when javascript-stacktrace is included
it('should include the stacktrace as an array with printStackTrace defined', function(){
expect(proxy.data.stack).to(be_an_instance_of, Array);
});

it('should return the string send in the stack trace', function(){
expect(jQuery).to(receive, 'ajax').with_args(
jQuery.extend(jQuery.flawed.config.ajax, {
data: {
url: jQuery(location).attr('href'),
stack: 'TypeError: "foo"'
}
})
);

expect_reraise();
it('should include the stack as the error object itself when printStackTrace is not defined', function(){
window.printStackTrace = undefined;
expect(proxy.data.stack).to(be_an_instance_of, Object);
});

it('should include the url from the current location', function(){
expect(proxy.data.url).to(be, $(location).attr('href'));
});

it('should include the type from the error name', function(){
expect(proxy.data.type).to(be, new Error('foo').name);
});

it('should include the message from the error', function(){
expect(proxy.data.message).to(be, new Error('foo').message);
});
});
});

Expand Down

0 comments on commit 9b8c7c4

Please sign in to comment.