Diff

net/http.lua @ 678:1859edec2237

Protected call for HTTP request callbacks, to catch errors
author Matthew Wild <mwild1@gmail.com>
date Thu, 08 Jan 2009 02:04:06 +0000
parent 677:93e5309c5430
child 714:ab3c47f4fe1d
line wrap: on
line diff
--- a/net/http.lua	Thu Jan 08 02:02:35 2009 +0000
+++ b/net/http.lua	Thu Jan 08 02:04:06 2009 +0000
@@ -9,7 +9,10 @@
 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 @@
 	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 @@
 		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);