# HG changeset patch # User jbe # Date 1500771073 -7200 # Node ID d89813dd4d922f537520ba46917acf41e37b69e9 # Parent 3b4dbabca31f0f1972550a5390280eeb77a59f50 New function request.add_error_handler(...); Allow layout_ident to be passed directly to slot.render_layout(...) diff -r 3b4dbabca31f -r d89813dd4d92 framework/env/request/__init.lua --- a/framework/env/request/__init.lua Sun Jul 02 04:22:20 2017 +0200 +++ b/framework/env/request/__init.lua Sun Jul 23 02:51:13 2017 +0200 @@ -32,5 +32,6 @@ request._forward_processed = false request._redirect = nil request._csrf_secret = nil + request._error_handlers = {} end) diff -r 3b4dbabca31f -r d89813dd4d92 framework/env/request/add_error_handler.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/request/add_error_handler.lua Sun Jul 23 02:51:13 2017 +0200 @@ -0,0 +1,17 @@ +--[[-- +request.add_error_handler( + function(errobj, stacktrace) + ... + end +) + +Registers a function to be called after an error occurred during request handling and the error response has been prepared by filling the "trace" and "system_error" slots in request.handler(...). The registered handler may, for example, send an error report to an administrator (utilizing slot.render_layout(...)). The passed handler function gets the error message (or error object) passed as first argument and the stacktrace as second argument. + +--]]-- + +function request.add_error_handler(func) + request.configure(function() + local handlers = request._error_handlers + handlers[#handlers+1] = func + end) +end diff -r 3b4dbabca31f -r d89813dd4d92 framework/env/request/handler.lua --- a/framework/env/request/handler.lua Sun Jul 02 04:22:20 2017 +0200 +++ b/framework/env/request/handler.lua Sun Jul 23 02:51:13 2017 +0200 @@ -261,6 +261,9 @@ slot.put(encode.html_newlines(encode.html(stacktrace))) slot.put("

") end) + for i, error_handler in ipairs(request._error_handlers) do + error_handler(error_info.errobj, error_info.stacktrace) + end elseif redirect_data then if redirect_data.include_tempstore == true or ( diff -r 3b4dbabca31f -r d89813dd4d92 framework/env/slot/render_layout.lua --- a/framework/env/slot/render_layout.lua Sun Jul 02 04:22:20 2017 +0200 +++ b/framework/env/slot/render_layout.lua Sun Jul 23 02:51:13 2017 +0200 @@ -1,20 +1,23 @@ --[[-- -output = -- document/data to be sent to the web browser -slot.render_layout() +output = -- document/data to be sent to the web browser +slot.render_layout( + layout_ident -- if set, selects layout to be used; otherwise layout set by slot.set_layout(...) is used +) This function returns the selected layout after replacing all slot placeholders with the respective slot contents. If slot.set_layout(...) was called with nil as first argument, then no layout will be used, but only the contents of the slot named "data" are returned. --]]-- -function slot.render_layout() - if slot._current_layout then +function slot.render_layout(layout_ident) + local layout_ident = layout_ident or slot._current_layout + if layout_ident then local layout_file = assert(io.open( encode.file_path( WEBMCP_BASE_PATH, 'app', WEBMCP_APP_NAME, '_layout', - slot._current_layout .. '.html' + layout_ident .. '.html' ), 'r' ))