From a92512707cbad6f73ba9e85353a40aac23e85358 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 7 Jul 2016 23:07:19 +0100 Subject: net.http: Add request.id to every request object (can be overridden by providing ex.id) --- net/http.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'net') diff --git a/net/http.lua b/net/http.lua index b78f8438..5fa0c0a9 100644 --- a/net/http.lua +++ b/net/http.lua @@ -27,6 +27,8 @@ local _ENV = nil; local requests = {}; -- Open requests +local function make_id(req) return (tostring(req):match("%x+$")); end + local listener = { default_port = 80, default_mode = "*a" }; function listener.onconnect(conn) @@ -125,6 +127,8 @@ local function request(u, ex, callback) req.path = "/"; end + req.id = ex and ex.id or make_id(req); + local method, headers, body; local host, port = req.host, req.port; -- cgit v1.2.3 From 2ba75409abf346221a10956cbc97da540de64deb Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 7 Jul 2016 23:08:47 +0100 Subject: net.http: Add log messages for requests, including their id (so "calling callback" and tracebacks can be traced back to their initial request) --- net/http.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'net') diff --git a/net/http.lua b/net/http.lua index 5fa0c0a9..669fa9a5 100644 --- a/net/http.lua +++ b/net/http.lua @@ -115,6 +115,13 @@ local function request_reader(request, data, err) end local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end +local function log_if_failed(id, ret, ...) + if not ret then + log("error", "Request %s: error in callback: %s", id, tostring((...))); + end + return ...; +end + local function request(u, ex, callback) local req = url.parse(u); @@ -165,6 +172,8 @@ local function request(u, ex, callback) end end + log("debug", "Making %s %s request %s to %s", req.scheme, method or "GET", req.id, (ex and ex.suppress_url and host_header) or u); + -- Attach to request object req.method, req.headers, req.body = method, headers, body; @@ -187,7 +196,10 @@ local function request(u, ex, callback) req.handler, req.conn = handler, conn req.write = function (...) return req.handler:write(...); end - req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end + req.callback = function (content, code, request, response) + log("debug", "request %s: Calling callback, status %s", req.id, code or "---"); + return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); + end req.reader = request_reader; req.state = "status"; -- cgit v1.2.3