webmcp
changeset 497:d89813dd4d92
New function request.add_error_handler(...); Allow layout_ident to be passed directly to slot.render_layout(...)
author | jbe |
---|---|
date | Sun Jul 23 02:51:13 2017 +0200 (2017-07-23) |
parents | 3b4dbabca31f |
children | e360b1933c78 |
files | framework/env/request/__init.lua framework/env/request/add_error_handler.lua framework/env/request/handler.lua framework/env/slot/render_layout.lua |
line diff
1.1 --- a/framework/env/request/__init.lua Sun Jul 02 04:22:20 2017 +0200 1.2 +++ b/framework/env/request/__init.lua Sun Jul 23 02:51:13 2017 +0200 1.3 @@ -32,5 +32,6 @@ 1.4 request._forward_processed = false 1.5 request._redirect = nil 1.6 request._csrf_secret = nil 1.7 + request._error_handlers = {} 1.8 1.9 end)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/framework/env/request/add_error_handler.lua Sun Jul 23 02:51:13 2017 +0200 2.3 @@ -0,0 +1,17 @@ 2.4 +--[[-- 2.5 +request.add_error_handler( 2.6 + function(errobj, stacktrace) 2.7 + ... 2.8 + end 2.9 +) 2.10 + 2.11 +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. 2.12 + 2.13 +--]]-- 2.14 + 2.15 +function request.add_error_handler(func) 2.16 + request.configure(function() 2.17 + local handlers = request._error_handlers 2.18 + handlers[#handlers+1] = func 2.19 + end) 2.20 +end
3.1 --- a/framework/env/request/handler.lua Sun Jul 02 04:22:20 2017 +0200 3.2 +++ b/framework/env/request/handler.lua Sun Jul 23 02:51:13 2017 +0200 3.3 @@ -261,6 +261,9 @@ 3.4 slot.put(encode.html_newlines(encode.html(stacktrace))) 3.5 slot.put("</p>") 3.6 end) 3.7 + for i, error_handler in ipairs(request._error_handlers) do 3.8 + error_handler(error_info.errobj, error_info.stacktrace) 3.9 + end 3.10 elseif redirect_data then 3.11 if 3.12 redirect_data.include_tempstore == true or (
4.1 --- a/framework/env/slot/render_layout.lua Sun Jul 02 04:22:20 2017 +0200 4.2 +++ b/framework/env/slot/render_layout.lua Sun Jul 23 02:51:13 2017 +0200 4.3 @@ -1,20 +1,23 @@ 4.4 --[[-- 4.5 -output = -- document/data to be sent to the web browser 4.6 -slot.render_layout() 4.7 +output = -- document/data to be sent to the web browser 4.8 +slot.render_layout( 4.9 + layout_ident -- if set, selects layout to be used; otherwise layout set by slot.set_layout(...) is used 4.10 +) 4.11 4.12 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. 4.13 4.14 --]]-- 4.15 4.16 -function slot.render_layout() 4.17 - if slot._current_layout then 4.18 +function slot.render_layout(layout_ident) 4.19 + local layout_ident = layout_ident or slot._current_layout 4.20 + if layout_ident then 4.21 local layout_file = assert(io.open( 4.22 encode.file_path( 4.23 WEBMCP_BASE_PATH, 4.24 'app', 4.25 WEBMCP_APP_NAME, 4.26 '_layout', 4.27 - slot._current_layout .. '.html' 4.28 + layout_ident .. '.html' 4.29 ), 4.30 'r' 4.31 ))