From 6b15ed47fd5bd8bc77186da4ef27384e933188c7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 8 Jan 2009 02:04:06 +0000 Subject: Protected call for HTTP request callbacks, to catch errors --- net/http.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'net') diff --git a/net/http.lua b/net/http.lua index 64d83948..2ff88269 100644 --- a/net/http.lua +++ b/net/http.lua @@ -9,7 +9,10 @@ local connlisteners_get = require "net.connlisteners".get; local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); local t_insert, t_concat = table.insert, table.concat; -local tonumber, tostring, pairs = tonumber, tostring, pairs; +local tonumber, tostring, pairs, xpcall, select, debug_traceback = + tonumber, tostring, pairs, xpcall, select, debug.traceback; + +local log = require "util.logger".init("http"); local print = function () end local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); @@ -105,6 +108,7 @@ local function request_reader(request, data, startpos) end end +local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end function request(u, ex, callback) local req = url.parse(u); @@ -157,9 +161,9 @@ function request(u, ex, callback) req.write(body); end - req.callback = callback; + req.callback = function (content, code, request) log("debug", "Calling callback, code %s content: %s", code or "---", content or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end req.reader = request_reader; - req.state = "status" + req.state = "status"; listener.register_request(req.handler, req); -- cgit v1.2.3