Skip to content

Commit

Permalink
[module] print better errors when module loading fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed May 5, 2017
1 parent 4f981ce commit 014ab7c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
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

0 comments on commit 014ab7c

Please sign in to comment.