Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

feature: now we print traceback to error.log when Lua errors happens …

…in log_by_lua*.
  • Loading branch information...
commit e876411bd201ab12734f6951f268b333c5b47894 1 parent 7ce7fb2
@agentzh agentzh authored
View
7 src/ngx_http_lua_logby.c
@@ -229,8 +229,13 @@ ngx_http_lua_log_by_chunk(lua_State *L, ngx_http_request_t *r)
old_pool = ngx_http_lua_pcre_malloc_init(r->pool);
#endif
+ lua_pushcfunction(L, ngx_http_lua_traceback);
+ lua_insert(L, 1); /* put it under chunk and args */
+
/* protected call user code */
- rc = lua_pcall(L, 0, 1, 0);
+ rc = lua_pcall(L, 0, 1, 1);
+
+ lua_remove(L, 1); /* remove traceback function */
#if (NGX_PCRE)
/* XXX: work-around to nginx regex subsystem */
View
26 src/ngx_http_lua_util.c
@@ -2387,6 +2387,32 @@ ngx_http_lua_thread_traceback(lua_State *L, lua_State *cc)
}
+int
+ngx_http_lua_traceback(lua_State *L)
+{
+ if (!lua_isstring(L, 1)) { /* 'message' not a string? */
+ return 1; /* keep it intact */
+ }
+
+ lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+ if (!lua_istable(L, -1)) {
+ lua_pop(L, 1);
+ return 1;
+ }
+
+ lua_getfield(L, -1, "traceback");
+ if (!lua_isfunction(L, -1)) {
+ lua_pop(L, 2);
+ return 1;
+ }
+
+ lua_pushvalue(L, 1); /* pass error message */
+ lua_pushinteger(L, 2); /* skip this function and traceback */
+ lua_call(L, 2, 1); /* call debug.traceback */
+ return 1;
+}
+
+
static void
ngx_http_lua_inject_arg_api(lua_State *L)
{
View
2  src/ngx_http_lua_util.h
@@ -117,6 +117,8 @@ ngx_chain_t * ngx_http_lua_chains_get_free_buf(ngx_log_t *log, ngx_pool_t *p,
void ngx_http_lua_create_new_global_table(lua_State *L, int narr, int nrec);
+int ngx_http_lua_traceback(lua_State *L);
+
#endif /* NGX_HTTP_LUA_UTIL_H */
View
31 t/075-logby.t
@@ -10,7 +10,7 @@ log_level('debug');
repeat_each(2);
-plan tests => repeat_each() * (blocks() * 3 + 4);
+plan tests => repeat_each() * (blocks() * 3 + 8);
#no_diff();
#no_long_string();
@@ -489,3 +489,32 @@ ok
--- error_log
API disabled in the context of log_by_lua*
+
+
+=== TEST 28: backtrace
+--- config
+ location /t {
+ echo ok;
+ log_by_lua '
+ function foo()
+ bar()
+ end
+
+ function bar()
+ error("something bad happened")
+ end
+
+ foo()
+ ';
+ }
+--- request
+ GET /t
+--- response_body
+ok
+--- error_log
+something bad happened
+stack traceback:
+in function 'error'
+in function 'bar'
+in function 'foo'
+
Please sign in to comment.
Something went wrong with that request. Please try again.