Skip to content

Commit 297e73c

Browse files
bugfix: the error message should use the first line rather than the last line of the code block when load lua code block failed. (openresty#2005)
1 parent 2615baf commit 297e73c

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

src/ngx_http_lua_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ struct ngx_http_lua_main_conf_s {
276276
of requests */
277277
ngx_uint_t malloc_trim_req_count;
278278

279+
ngx_uint_t directive_line;
280+
279281
#if (nginx_version >= 1011011)
280282
/* the following 2 fields are only used by ngx.req.raw_headers() for now */
281283
ngx_buf_t **busy_buf_ptrs;

src/ngx_http_lua_directive.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,9 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13021302
{
13031303
u_char *p, *out;
13041304
size_t len;
1305+
ngx_uint_t start_line;
1306+
1307+
ngx_http_lua_main_conf_t *lmcf;
13051308

13061309
len = sizeof("=(:)") - 1 + tag_len + cf->conf_file->file.name.len
13071310
+ NGX_INT64_LEN + 1;
@@ -1328,10 +1331,14 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13281331

13291332
found:
13301333

1334+
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
1335+
start_line = lmcf->directive_line > 0
1336+
? lmcf->directive_line : cf->conf_file->line;
1337+
13311338
p = ngx_snprintf(out, len, "=%*s(%*s:%d)%Z",
13321339
tag_len, tag, cf->conf_file->file.name.data
13331340
+ cf->conf_file->file.name.len - p,
1334-
p, cf->conf_file->line);
1341+
p, start_line);
13351342

13361343
*chunkname_len = p - out - 1; /* exclude the trailing '\0' byte */
13371344

@@ -1343,6 +1350,7 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13431350
char *
13441351
ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd)
13451352
{
1353+
ngx_http_lua_main_conf_t *lmcf;
13461354
ngx_http_lua_block_parser_ctx_t ctx;
13471355

13481356
int level = 1;
@@ -1376,6 +1384,9 @@ ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd)
13761384
ctx.token_len = 0;
13771385
start_line = cf->conf_file->line;
13781386

1387+
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
1388+
lmcf->directive_line = start_line;
1389+
13791390
dd("init start line: %d", (int) start_line);
13801391

13811392
ctx.start_line = start_line;
@@ -1494,6 +1505,8 @@ ngx_http_lua_conf_lua_block_parse(ngx_conf_t *cf, ngx_command_t *cmd)
14941505

14951506
done:
14961507

1508+
lmcf->directive_line = 0;
1509+
14971510
if (rc == NGX_ERROR) {
14981511
return NGX_CONF_ERROR;
14991512
}

t/002-content.t

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Test::Nginx::Socket::Lua;
1010
repeat_each(2);
1111
#repeat_each(1);
1212

13-
plan tests => repeat_each() * (blocks() * 2 + 24);
13+
plan tests => repeat_each() * (blocks() * 2 + 27);
1414

1515
#no_diff();
1616
#no_long_string();
@@ -849,4 +849,71 @@ GET /lua
849849
--- response_body_like: 500 Internal Server Error
850850
--- error_code: 500
851851
--- error_log eval
852-
qr/failed to load inlined Lua code: /
852+
qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:40\)/
853+
854+
855+
856+
=== TEST 43: syntax error in content_by_lua_block
857+
--- config
858+
location /lua {
859+
860+
content_by_lua_block {
861+
'for end';
862+
}
863+
}
864+
--- request
865+
GET /lua
866+
--- response_body_like: 500 Internal Server Error
867+
--- error_code: 500
868+
--- error_log eval
869+
qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:41\)/
870+
871+
872+
873+
=== TEST 44: syntax error in second content_by_lua_block
874+
--- config
875+
location /foo {
876+
content_by_lua_block {
877+
'for end';
878+
}
879+
}
880+
881+
location /lua {
882+
content_by_lua_block {
883+
'for end';
884+
}
885+
}
886+
--- request
887+
GET /lua
888+
--- response_body_like: 500 Internal Server Error
889+
--- error_code: 500
890+
--- error_log eval
891+
qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:46\)/
892+
893+
894+
895+
=== TEST 45: syntax error in thrid content_by_lua_block
896+
--- config
897+
location /foo {
898+
content_by_lua_block {
899+
'for end';
900+
}
901+
}
902+
903+
location /bar {
904+
content_by_lua_block {
905+
'for end';
906+
}
907+
}
908+
909+
location /lua {
910+
content_by_lua_block {
911+
'for end';
912+
}
913+
}
914+
--- request
915+
GET /lua
916+
--- response_body_like: 500 Internal Server Error
917+
--- error_code: 500
918+
--- error_log eval
919+
qr/failed to load inlined Lua code: content_by_lua\(nginx.conf:52\)/

0 commit comments

Comments
 (0)