From cdffbb372e7aaac2981a992214e4f9c5034aec7b Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 24 Feb 2015 00:25:48 -0800 Subject: [PATCH 1/3] Better error handling --- src/kong/core/access.lua | 1 + src/kong/plugins/authentication/access.lua | 4 ++-- src/kong/plugins/ratelimiting/access.lua | 6 ++++-- src/kong/web/routes/base_controller.lua | 4 ++-- src/main.lua | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/kong/core/access.lua b/src/kong/core/access.lua index 4b3707e6fa4b..718d67375990 100644 --- a/src/kong/core/access.lua +++ b/src/kong/core/access.lua @@ -25,6 +25,7 @@ function _M.execute(conf) -- Retrieving the API from the Host that has been requested local apis, err = dao.apis:find_by_keys({public_dns = stringy.split(ngx.var.http_host, ":")[1]}) if err then + ngx.log(ngx.ERR, err.message) utils.show_error(500) elseif not apis or #apis == 0 then utils.not_found("API not found") diff --git a/src/kong/plugins/authentication/access.lua b/src/kong/plugins/authentication/access.lua index 80a3dc68dadb..1a38b0945e97 100644 --- a/src/kong/plugins/authentication/access.lua +++ b/src/kong/plugins/authentication/access.lua @@ -163,8 +163,8 @@ function _M.execute(conf) if public_key then local applications, err = dao.applications:find_by_keys { public_key = public_key } if err then - ngx.log(ngx.ERR, err) - return + ngx.log(ngx.ERR, err.message) + utils.show_error(500) elseif #applications > 0 then application = applications[1] end diff --git a/src/kong/plugins/ratelimiting/access.lua b/src/kong/plugins/ratelimiting/access.lua index d849106f64ca..d178fda81bcf 100644 --- a/src/kong/plugins/ratelimiting/access.lua +++ b/src/kong/plugins/ratelimiting/access.lua @@ -16,7 +16,8 @@ function _M.execute(conf) -- Load current metric for configured period local current_metric, err = dao.metrics:find_one(ngx.ctx.api.id, identifier, current_timestamp, conf.period) if err then - ngx.log(ngx.ERROR, err) + ngx.log(ngx.ERROR, err.message) + utils.show_error(500) end -- What is the current usage for the configured period? @@ -38,7 +39,8 @@ function _M.execute(conf) -- Increment metrics for all periods if the request goes through local _, err = dao.metrics:increment(ngx.ctx.api.id, identifier, current_timestamp) if err then - ngx.log(ngx.ERROR, err) + ngx.log(ngx.ERROR, err.message) + utils.show_error(500) end end diff --git a/src/kong/web/routes/base_controller.lua b/src/kong/web/routes/base_controller.lua index e8a66e8cb8d6..b22749b89002 100644 --- a/src/kong/web/routes/base_controller.lua +++ b/src/kong/web/routes/base_controller.lua @@ -47,8 +47,9 @@ local function parse_params(dao_collection, params) end local function parse_dao_error(err) - local status + ngx.log(ngx.ERR, err.message) + local status if err.database then status = 500 elseif err.unique then @@ -60,7 +61,6 @@ local function parse_dao_error(err) else status = 400 end - return utils.show_error(status, err.message) end diff --git a/src/main.lua b/src/main.lua index 4ba3fceca7c5..38e8051a472b 100644 --- a/src/main.lua +++ b/src/main.lua @@ -41,7 +41,7 @@ local function load_plugin_conf(api_id, application_id, plugin_name) if err then ngx.log(ngx.ERROR, err) - return nil + utils.show_error(500) end if #rows > 0 then From 03763495173b7dcb4e259172af62c013d7b7f3e6 Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 24 Feb 2015 00:32:29 -0800 Subject: [PATCH 2/3] logging error in nginx log only if 500 --- src/kong/web/routes/base_controller.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/kong/web/routes/base_controller.lua b/src/kong/web/routes/base_controller.lua index b22749b89002..4abb0966e54e 100644 --- a/src/kong/web/routes/base_controller.lua +++ b/src/kong/web/routes/base_controller.lua @@ -47,11 +47,10 @@ local function parse_params(dao_collection, params) end local function parse_dao_error(err) - ngx.log(ngx.ERR, err.message) - local status if err.database then status = 500 + ngx.log(ngx.ERR, err.message) elseif err.unique then status = 409 elseif err.foreign then From 1e5ef4dd2dfcddf35329401938de54c8acbe7a2c Mon Sep 17 00:00:00 2001 From: thefosk Date: Tue, 24 Feb 2015 00:35:51 -0800 Subject: [PATCH 3/3] Showing default error --- src/kong/tools/utils.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/kong/tools/utils.lua b/src/kong/tools/utils.lua index 8c1df67f70a5..bb8a18295ac6 100644 --- a/src/kong/tools/utils.lua +++ b/src/kong/tools/utils.lua @@ -145,6 +145,9 @@ end function _M.show_error(status, message) ngx.ctx.error = true + if not message then + message = "An error occurred" + end _M.show_response(status, message) end