Changeset

6206:ac7e2992fe6e

mod_rest: Strip down error payloads to avoid stack overflow When the util.error feature of including a traceback in the error, util.json can't serialize it as it is often self-referential.
author Kim Alvefur <zash@zash.se>
date Sun, 16 Mar 2025 17:04:51 +0100
parents 6205:8ff8121ff603
children 6207:a1a33f0f6f6e
files mod_rest/mod_rest.lua
diffstat 1 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Thu Mar 13 13:00:26 2025 +0000
+++ b/mod_rest/mod_rest.lua	Sun Mar 16 17:04:51 2025 +0100
@@ -662,6 +662,17 @@
 	"application/json",
 };
 
+-- strip some stuff, notably the optional traceback table that casues stack overflow in util.json
+local function simplify_error(e)
+	return {
+		type = e.type;
+		condition = e.condition;
+		text = e.text;
+		extra = e.extra;
+		source = e.source;
+	};
+end
+
 local http_server = require "net.http.server";
 module:hook_object_event(http_server, "http-error", function (event)
 	local request, response = event.request, event.response;
@@ -684,7 +695,7 @@
 		end
 		return json.encode({
 				type = "error",
-				error = event.error,
+				error = simplify_error(event.error),
 				code = event.code,
 			});
 	end