diff src/luan/modules/http/HttpServicer.java @ 1263:382c444a6c77

add Http.eval_in_root and Http.handle_error
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 24 Sep 2018 22:06:25 -0600
parents c147e2e877e3
children 3f4644246e39
line wrap: on
line diff
--- a/src/luan/modules/http/HttpServicer.java	Mon Sep 24 14:48:59 2018 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Mon Sep 24 22:06:25 2018 -0600
@@ -13,31 +13,43 @@
 import luan.modules.PackageLuan;
 
 
-public class HttpServicer {
+public final class HttpServicer {
 	private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class);
 
-	public Response service(LuanState luan,Request request,String modName) {
+	public static Response service(LuanState luan,Request request,String modName) {
 		try {
 			return serviceLuan(luan,request,modName);
 		} catch(LuanException e) {
-			return handleError(request,e);
+			return handleError(luan,request,e);
 		}
 	}
 
-	protected Response handleError(Request request,LuanException e) {
+	private static Response handleError(LuanState luan,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 );
+*/
+		synchronized(luan) {
+			LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
+			luan = (LuanState)cloner.clone(luan);
+		}
+		try {
+			LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
+			LuanFunction fn = (LuanFunction)module.rawGet("handle_error");
+			return (Response)fn.call( luan, new Object[]{request,e.table()} );
+		} catch(LuanException e2) {
+			throw new RuntimeException(e2);
+		}
 	}
 
-	protected Response serviceLuan(LuanState luan,Request request,String modName)
+	private static 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);
@@ -67,8 +79,5 @@
 		return response;
 	}
 
-	protected void inSinchronized(LuanState luan,Request request)
-		throws LuanException
-	{}
-
+	private HttpServicer() {}  // never
 }