Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[module] print better errors when module loading fails #360

Merged
merged 1 commit into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Experimental caching proxy to the http client [PR #357](https://github.com/3scale/apicast/pull/357)

### Changed

- Print better errors when module loading fails [PR #360](https://github.com/3scale/apicast/pull/360)

## [3.0.0-rc1] - 2017-04-04

### Added
Expand Down
24 changes: 16 additions & 8 deletions apicast/src/module.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
local pcall = pcall
local xpcall = xpcall
local require = require
local dofile = dofile
local type = type

local env = require 'resty.env'

local function error_message(error)
ngx.log(ngx.WARN, error)
return error
end

local prequire = function(file)
local ok, ret = pcall(require, file)
local ok, ret = xpcall(require, error_message, file)

if not ok and ret then
-- dofile can load absolute paths, require can't
ok, ret = pcall(dofile, file)
ok, ret = xpcall(dofile, error_message, file)
end

if not ok and ret then
if type(ret) == 'userdata' then
ngx.log(ngx.WARN, 'cyclic require detected: ', debug.traceback())
elseif type(ret) == 'string' then
ngx.log(ngx.WARN, ret, ', ', debug.traceback())
end
return false, ret
end
Expand All @@ -30,10 +33,15 @@ local name = env.get('APICAST_MODULE') or 'apicast'
local ok, mod = prequire(name)

if ok and mod then
if mod.new then
return mod.new()
if type(mod) == 'table' then
if mod.new then
return mod.new()
else
return mod
end
else
return mod
ngx.log(ngx.ERR, 'module ', name, ' did not return a table but: ', type(mod))
return false
end
else
return ok, mod
Expand Down
63 changes: 63 additions & 0 deletions t/018-module-loading.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use Test::Nginx::Socket 'no_plan';
use Cwd qw(cwd);

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

$ENV{TEST_NGINX_LUA_PATH} = "$pwd/t/servroot/html/?.lua;$apicast/src/?.lua;;";
$ENV{TEST_NGINX_HTTP_CONFIG} = "$apicast/http.d/init.conf";
$ENV{TEST_NGINX_APICAST_PATH} = $apicast;
$ENV{APICAST_MODULE} = 'custom';

env_to_nginx(
'APICAST_MODULE'
);

log_level('warn');
repeat_each(2);
no_root_location();
run_tests();

__DATA__

=== TEST 1: print error when file is missing

--- http_config
include $TEST_NGINX_HTTP_CONFIG;
lua_package_path "$TEST_NGINX_LUA_PATH";
--- config
--- must_die
--- request
GET
--- error_log
module 'custom' not found


=== TEST 2: print error on syntax error

--- http_config
include $TEST_NGINX_HTTP_CONFIG;
lua_package_path "$TEST_NGINX_LUA_PATH";
--- config
--- must_die
--- request
GET
--- error_log
custom.lua:1: unexpected symbol near 'not'
--- user_files
>>> custom.lua
not valid lua

=== TEST 3: print error on empty file

--- http_config
include $TEST_NGINX_HTTP_CONFIG;
lua_package_path "$TEST_NGINX_LUA_PATH";
--- config
--- request
GET
--- error_log
module custom did not return a table but: boolean
--- must_die
--- user_files
>>> custom.lua