Changeset

4161:c299726d2b4e

net.httpserver: Catch errors thrown in HTTP handlers.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 23 Feb 2011 02:03:55 +0500
parents 4160:f08f649b898b
children 4162:af720a91aa19
files net/httpserver.lua
diffstat 1 files changed, 8 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/net/httpserver.lua	Wed Feb 23 01:34:46 2011 +0500
+++ b/net/httpserver.lua	Wed Feb 23 02:03:55 2011 +0500
@@ -17,6 +17,8 @@
 
 local t_insert, t_concat = table.insert, table.concat;
 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
+local xpcall = xpcall;
+local debug_traceback = debug.traceback;
 
 local urlencode = function (s) return s and (s:gsub("%W", function (c) return ("%%%02x"):format(c:byte()); end)); end
 
@@ -86,6 +88,12 @@
 		callback = (request.server and request.server.handlers[base]) or default_handler;
 	end
 	if callback then
+		local _callback = callback;
+		function callback(a, b, c)
+			local status, result = xpcall(function() _callback(a, b, c) end, debug_traceback);
+			if status then return result; end
+			log("error", "Error in HTTP server handler: %s", result);
+		end
 		if err then
 			log("debug", "Request error: "..err);
 			if not callback(nil, err, request) then