diff src/luan/modules/http/HttpServicer.java @ 1256:c147e2e877e3

allow subclassing of HttpServicer
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Sep 2018 20:15:16 -0600
parents 354e661dee7f
children 382c444a6c77
line wrap: on
line diff
--- a/src/luan/modules/http/HttpServicer.java	Mon Sep 03 06:13:55 2018 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Wed Sep 19 20:15:16 2018 -0600
@@ -4,6 +4,7 @@
 import org.slf4j.LoggerFactory;
 import luan.webserver.Request;
 import luan.webserver.Response;
+import luan.webserver.Status;
 import luan.LuanState;
 import luan.LuanFunction;
 import luan.LuanException;
@@ -12,14 +13,31 @@
 import luan.modules.PackageLuan;
 
 
-public final class HttpServicer {
+public class HttpServicer {
 	private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class);
 
-	public static Response service(LuanState luan,Request request,String modName)
+	public Response service(LuanState luan,Request request,String modName) {
+		try {
+			return serviceLuan(luan,request,modName);
+		} catch(LuanException e) {
+			return handleError(request,e);
+		}
+	}
+
+	protected Response handleError(Request request,LuanException e) {
+//e.printStackTrace();
+		String err = e.getLuanStackTraceString();
+		logger.error(err+"\n"+request.rawHead.trim()+"\n");
+		String msg = "Internel Server Error\n\n" + err;
+		return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg );
+	}
+
+	protected Response serviceLuan(LuanState luan,Request request,String modName)
 		throws LuanException
 	{
 		LuanFunction fn;
 		synchronized(luan) {
+			inSinchronized(luan,request);
 			PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName);
 			PackageLuan.require(luan,"luan:http/Http.luan");
 			Object mod = PackageLuan.load(luan,modName);
@@ -49,4 +67,8 @@
 		return response;
 	}
 
+	protected void inSinchronized(LuanState luan,Request request)
+		throws LuanException
+	{}
+
 }