diff --git a/CHANGELOG.md b/CHANGELOG.md index aa4defdc..b91d0214 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed - Average collector +### Fixed +- Throw an error when http_middelware is processing a wrong handler [#199](https://github.com/tarantool/metrics/issues/199) + ## [0.10.0] - 2021-08-03 ### Changed - metrics registry refactoring to search with `O(1)` [#188](https://github.com/tarantool/metrics/issues/188) diff --git a/metrics/http_middleware.lua b/metrics/http_middleware.lua index f379a036..ce6a629e 100644 --- a/metrics/http_middleware.lua +++ b/metrics/http_middleware.lua @@ -71,6 +71,10 @@ end -- ... arguments for pcall to instrument function export.observe(collector, route, ...) return collector:observe_latency(function(ok, result) + if type(result) ~= 'table' then + error(('incorrect http handler for %s %s: expecting return response object'): + format(route.method, route.path), 0) + end return { path = route.path, method = route.method, diff --git a/test/http_middleware_test.lua b/test/http_middleware_test.lua index 97ff6ba8..e4dfcacc 100644 --- a/test/http_middleware_test.lua +++ b/test/http_middleware_test.lua @@ -148,6 +148,20 @@ g.test_v1_middleware = function() ) end +g.test_v1_wrong_handler = function() + local request = {endpoint = table.copy(route)} + + local observer = stub_observer(function() end) + local handler = function() + -- we forget to return result! + -- return result + end + t.assert_error_msg_contains( + 'incorrect http handler for POST /some/path: expecting return response object', + http_middleware.v1(handler, observer), request + ) +end + g.test_v2_middleware = function() local httpd = require('http.server').new('127.0.0.1', 12345) t.skip_if(httpd.set_router == nil, 'Skip http 2.x test') @@ -170,3 +184,24 @@ g.test_v2_middleware = function() 'number' ) end + +g.test_v2_wrong_handler = function() + local httpd = require('http.server').new('127.0.0.1', 12345) + t.skip_if(httpd.set_router == nil, 'Skip http 2.x test') + local router = require('http.router').new() + router:route(route, function() + -- we forget to return result! + -- return result + end) + router:use(http_middleware.v2(), {name = 'http_instrumentation'}) + httpd:set_router(router) + httpd:start() + local client = require('http.client').new() + local response = client:request(route.method, 'http://127.0.0.1:12345' .. route.path) + httpd:stop() + t.assert_equals(response.status, 500) + t.assert_str_contains( + response.body, + 'incorrect http handler for POST /some/path: expecting return response object' + ) +end