<h3>Description</h3>
<p>Hello,</p>
<p>When there is an error in my ruby script, app_ruby prints exception message without a stack trace, like this:</p>
<pre><code>Dec 14 16:25:17 servername /usr/sbin/kamailio[24313]: ERROR: app_ruby [app_ruby_api.c:105]: app_ruby_print_last_exception(): exception: wrong number of arguments (1 for 2..3)
Dec 14 16:25:17 servername /usr/sbin/kamailio[24313]: ERROR: app_ruby [app_ruby_api.c:1078]: app_ruby_run_ex(): ruby exception (6) on callback for: ksr_request_route (res type: 17)
</code></pre>
<p>Altough this message tells what is the error, it doesn't tell <strong>where</strong> the error happened, so it's hard to find the source of the problem.</p>
<p>In ruby, exceptions have <em>backtrace()</em> method returning stack trace as an array of strings. It would be really helpful if app_ruby also call this method and append its result to error message.</p>
<h3>Reproduction</h3>
<p>in kamailio.cfg</p>
<pre><code>loadmodule "app_ruby.so"
modparam("app_ruby", "load", "/etc/kamailio/ruby/routes.rb")
cfgengine "ruby"
</code></pre>
<p>in /etc/kamailio/ruby/routes.rb</p>
<pre><code>def some_method(param1, param2)
end

def ksr_request_route
  some_method(1)
end
</code></pre>
<h3>Expected behavior</h3>
<pre><code>Dec 14 16:46:24 servername /usr/sbin/kamailio[24314]: ERROR: <core> [core/kemi.c:87]: sr_kemi_core_err(): wrong number of arguments (1 for 2): /etc/kamailio/ruby/routes.rb:1:in `some_method'
                                                                     /etc/kamailio/ruby/routes.rb:4:in `ksr_request_route'
</code></pre>
<h4>Actual observed behavior</h4>
<pre><code>Dec 14 16:45:42 servername /usr/sbin/kamailio[24311]: ERROR: app_ruby [app_ruby_api.c:105]: app_ruby_print_last_exception(): exception: wrong number of arguments (1 for 2)
</code></pre>
<h3>Possible Solutions</h3>
<p>Relevant code seem to be here: <a href="https://github.com/kamailio/kamailio/blob/master/src/modules/app_ruby/app_ruby_api.c#L98">https://github.com/kamailio/kamailio/blob/master/src/modules/app_ruby/app_ruby_api.c#L98</a></p>
<p>I found example C code to print stack trace here but couldn't figure out how to integrate to app_ruby: <a href="http://zoo-project.org/svn/trunk/zoo-project/zoo-kernel/service_internal_ruby.c" rel="nofollow">http://zoo-project.org/svn/trunk/zoo-project/zoo-kernel/service_internal_ruby.c</a></p>
<p>Equivalent ruby code would be (this code is not idiomatic, my aim is to provide C-like ruby code):</p>
<pre><code>def some_error_generating_method
  method_taking_two_params(1)
rescue => exception
  message = exception.to_s() + "\n"
  stack_trace = exception.backtrace()
  for line in stack_trace
    message = message + line + "\n"
  end
  print(message)
end
</code></pre>
<h3>Additional Information</h3>
<ul>
<li><strong>Kamailio Version</strong> - output of <code>kamailio -v</code></li>
</ul>
<pre><code>version: kamailio 5.2.0 (x86_64/linux) 535e13
flags: STATS: Off, USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MEM, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLACKLIST, HAVE_RESOLV_RES
ADAPTIVE_WAIT_LOOPS=1024, MAX_RECV_BUFFER_SIZE 262144 MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: 535e13
compiled on 10:26:34 Nov 28 2018 with gcc 4.8.5
</code></pre>
<ul>
<li><strong>Operating System</strong>:</li>
</ul>
<p>Centos 7.2 64bit</p>
<pre><code>Linux servername 3.10.0-327.28.2.el7.x86_64 #1 SMP Wed Aug 3 11:11:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1766">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZeMFoLorVbG5D0613J0wUP057Zfoks5u47FcgaJpZM4ZTmQw">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZUqU4s1cJScVZMyYsOJ5XlIktV1mks5u47FcgaJpZM4ZTmQw.gif" height="1" width="1" alt="" /></p>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://github.githubassets.com/images/email/message_cards/header.png","avatar_image_url":"https://github.githubassets.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"app_ruby: print backtrace when ruby exception occurs (#1766)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1766"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/1766",
"url": "https://github.com/kamailio/kamailio/issues/1766",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>