Skip to content

Commit

Permalink
warn on exceptions in success/failure/complete callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd Fisher committed Nov 15, 2011
1 parent 3475b88 commit 624b399
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 11 additions & 5 deletions ext/curb_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static void rb_curl_mutli_handle_complete(VALUE self, CURL *easy_handle, int res
long response_code = -1;
VALUE easy;
ruby_curl_easy *rbce = NULL;
VALUE callargs;
VALUE callargs, val = Qtrue;

CURLcode ecode = curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, (char**)&easy);

Expand All @@ -354,7 +354,7 @@ static void rb_curl_mutli_handle_complete(VALUE self, CURL *easy_handle, int res

if (!rb_easy_nil("complete_proc")) {
callargs = rb_ary_new3(2, rb_easy_get("complete_proc"), easy);
rb_rescue(call_status_handler1, callargs, callback_exception, Qnil);
val = rb_rescue(call_status_handler1, callargs, callback_exception, Qnil);
//rb_funcall( rb_easy_get("complete_proc"), idCall, 1, easy );
}

Expand All @@ -363,24 +363,30 @@ static void rb_curl_mutli_handle_complete(VALUE self, CURL *easy_handle, int res
if (result != 0) {
if (!rb_easy_nil("failure_proc")) {
callargs = rb_ary_new3(3, rb_easy_get("failure_proc"), easy, rb_curl_easy_error(result));
rb_rescue(call_status_handler2, callargs, callback_exception, Qnil);
val = rb_rescue(call_status_handler2, callargs, callback_exception, Qnil);
//rb_funcall( rb_easy_get("failure_proc"), idCall, 2, easy, rb_curl_easy_error(result) );
}
}
else if (!rb_easy_nil("success_proc") &&
((response_code >= 200 && response_code < 300) || response_code == 0)) {
/* NOTE: we allow response_code == 0, in the case of non http requests e.g. reading from disk */
callargs = rb_ary_new3(2, rb_easy_get("success_proc"), easy);
rb_rescue(call_status_handler1, callargs, callback_exception, Qnil);
val = rb_rescue(call_status_handler1, callargs, callback_exception, Qnil);
//rb_funcall( rb_easy_get("success_proc"), idCall, 1, easy );
}
else if (!rb_easy_nil("failure_proc") &&
(response_code >= 300 && response_code <= 999)) {
callargs = rb_ary_new3(3, rb_easy_get("failure_proc"), easy, rb_curl_easy_error(result));
rb_rescue(call_status_handler2, callargs, callback_exception, Qnil);
val = rb_rescue(call_status_handler2, callargs, callback_exception, Qnil);
//rb_funcall( rb_easy_get("failure_proc"), idCall, 2, easy, rb_curl_easy_error(result) );
}

if (val == Qfalse) {
rb_warn("uncaught exception from callback");
// exception was raised?
//fprintf(stderr, "exception raised from callback\n");
}

}

static void rb_curl_multi_read_info(VALUE self, CURLM *multi_handle) {
Expand Down
13 changes: 10 additions & 3 deletions tests/tc_curl_easy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ def test_get_set_multi_on_easy
assert_equal multi, easy.multi
end

def test_handle_exceptions_in_progress_callbacks
def test_raise_on_progress
c = Curl::Easy.new($TEST_URL)
c.on_progress {|x| raise "error" }
c.perform
Expand All @@ -927,15 +927,22 @@ def test_handle_exceptions_in_progress_callbacks
c.close
end

def test_handle_exceptions_in_debug_callbacks
def test_raise_on_success
c = Curl::Easy.new($TEST_URL)
c.on_debug {|x| raise "error" }
c.on_success {|x| raise "error" }
c.perform
rescue => e
assert_equal 'Curl::Err::AbortedByCallbackError', e.class.to_s
c.close
end

def test_raise_on_debug
c = Curl::Easy.new($TEST_URL)
c.on_debug {|x| raise "error" }
c.perform
assert true, "raise in on debug has no effect"
end

include TestServerMethods

def setup
Expand Down

0 comments on commit 624b399

Please sign in to comment.