Skip to content

Commit

Permalink
refactor(api) better way of detecting 405s
Browse files Browse the repository at this point in the history
- Catch 405s for all endpoints
- Remove the old API tests since the API now seems stable enough
  • Loading branch information
thibaultcha committed Jun 26, 2015
1 parent ebcf8a0 commit 38f1b7f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 458 deletions.
21 changes: 6 additions & 15 deletions kong/api/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,14 @@ app.handle_404 = function(self)
end

app.handle_error = function(self, err, trace)
ngx.log(ngx.ERR, err.."\n"..trace)

local iterator, iter_err = ngx.re.gmatch(err, ".+:\\d+:\\s*(.+)")
if iter_err then
ngx.log(ngx.ERR, iter_err)
end

local m, iter_err = iterator()
if iter_err then
ngx.log(ngx.ERR, iter_err)
end

if m and table.getn(m) > 0 then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(m[1])
if stringy.find(err, "don't know how to respond to") ~= nil then
return responses.send_HTTP_METHOD_NOT_ALLOWED()
else
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
ngx.log(ngx.ERR, err.."\n"..trace)
end

-- We just logged the error so no need to give it to responses and log it twice
return responses.send_HTTP_INTERNAL_SERVER_ERROR()
end

local handler_helpers = {
Expand Down
14 changes: 0 additions & 14 deletions kong/api/routes/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ local function get_hostname()
return hostname
end

local METHOD_NOT_ALLOWED_MESSAGE = { message = "Method not allowed" }

return {
["/"] = {
GET = function(self, dao, helpers)
Expand All @@ -28,18 +26,6 @@ return {
},
lua_version = jit and jit.version or _VERSION
})
end,
POST = function(self, dao, helpers)
return helpers.responses.send_HTTP_METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_MESSAGE)
end,
PUT = function(self, dao, helpers)
return helpers.responses.send_HTTP_METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_MESSAGE)
end,
PATCH = function(self, dao, helpers)
return helpers.responses.send_HTTP_METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_MESSAGE)
end,
DELETE = function(self, dao, helpers)
return helpers.responses.send_HTTP_METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_MESSAGE)
end
}
}
7 changes: 6 additions & 1 deletion kong/tools/responses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ local response_default_content = {
end,
[_M.status_codes.HTTP_INTERNAL_SERVER_ERROR] = function(content)
return "An unexpected error occurred"
end,
[_M.status_codes.HTTP_METHOD_NOT_ALLOWED] = function(content)
return "Method not allowed"
end
}

Expand All @@ -50,7 +53,9 @@ local function send_response(status_code)

return function(content, raw)
if status_code >= _M.status_codes.HTTP_INTERNAL_SERVER_ERROR then
ngx.log(ngx.ERR, tostring(content))
if content then
ngx.log(ngx.ERR, tostring(content))
end
ngx.ctx.stop_phases = true -- interrupt other phases of this request
end

Expand Down
Loading

0 comments on commit 38f1b7f

Please sign in to comment.