mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-06 18:00:28 +00:00
New OnError(status, message) hook + documentation
This commit is contained in:
parent
6a76293c4f
commit
9d89be2a59
3 changed files with 25 additions and 16 deletions
|
@ -550,6 +550,17 @@ SPECIAL PATHS
|
||||||
---
|
---
|
||||||
function OnHttpRequest() end
|
function OnHttpRequest() end
|
||||||
|
|
||||||
|
--- Hooks catch errors
|
||||||
|
---
|
||||||
|
--- If this functiopn is defined in the global scope by your `/.init.lua`
|
||||||
|
--- then any errors occuring in the OnHttpRequest() hook will be catched.
|
||||||
|
--- You'll be able then to do whatever you need with the error status and
|
||||||
|
--- error message.
|
||||||
|
---
|
||||||
|
---@param status uint16
|
||||||
|
---@param message string
|
||||||
|
function OnError(status, message) end
|
||||||
|
|
||||||
--- Hooks client connection creation.
|
--- Hooks client connection creation.
|
||||||
---
|
---
|
||||||
--- If this function is defined it'll be called from the main process
|
--- If this function is defined it'll be called from the main process
|
||||||
|
|
|
@ -575,11 +575,13 @@ HOOKS
|
||||||
hand over control for all messages (with the exception of OPTIONS
|
hand over control for all messages (with the exception of OPTIONS
|
||||||
*). See functions like Route which asks redbean to do its default
|
*). See functions like Route which asks redbean to do its default
|
||||||
thing from the handler.
|
thing from the handler.
|
||||||
OnError()
|
|
||||||
|
OnError(status:int, message:string)
|
||||||
If this function is defined and if any errors occurs in
|
If this function is defined and if any errors occurs in
|
||||||
OnHttpRequest() then this method will be called instead of
|
OnHttpRequest() then this method will be called instead of displaying
|
||||||
displaying the default 500 page. Useful if you need to display
|
the default error page. Useful if you need to display the error page
|
||||||
the error page using your specific code.
|
using your specific code or send it to any tier service.
|
||||||
|
|
||||||
OnClientConnection(ip:int, port:int, serverip:int, serverport:int) → bool
|
OnClientConnection(ip:int, port:int, serverip:int, serverport:int) → bool
|
||||||
If this function is defined it'll be called from the main process
|
If this function is defined it'll be called from the main process
|
||||||
each time redbean accepts a new client connection. If it returns
|
each time redbean accepts a new client connection. If it returns
|
||||||
|
|
|
@ -3253,29 +3253,25 @@ static char *LuaOnHttpRequest(void) {
|
||||||
if (LuaCallWithYield(L) == LUA_OK) {
|
if (LuaCallWithYield(L) == LUA_OK) {
|
||||||
return CommitOutput(GetLuaResponse());
|
return CommitOutput(GetLuaResponse());
|
||||||
} else {
|
} else {
|
||||||
errormessage = lua_tostring(L, -1);
|
LogLuaError("OnHttpRequest", lua_tostring(L, -1));
|
||||||
LogLuaError("OnHttpRequest", errormessage);
|
|
||||||
|
|
||||||
if (hasonerror) {
|
if (hasonerror) {
|
||||||
lua_pushstring(L, errormessage);
|
|
||||||
lua_setglobal(L, errormessage);
|
|
||||||
|
|
||||||
lua_settop(L, 0);
|
|
||||||
lua_getglobal(L, "OnError");
|
lua_getglobal(L, "OnError");
|
||||||
if (LuaCallWithYield(L) == LUA_OK) {
|
lua_pushinteger(L, 500);
|
||||||
|
lua_pushstring(L, errormessage);
|
||||||
|
if (LuaCallWithTrace(L, 2, 0, NULL) == LUA_OK) {
|
||||||
return CommitOutput(GetLuaResponse());
|
return CommitOutput(GetLuaResponse());
|
||||||
} else {
|
} else {
|
||||||
error = ServeErrorWithDetail(
|
error = ServeErrorWithDetail(
|
||||||
500, "Internal Server Error",
|
500, "Internal Server Error!!",
|
||||||
ShouldServeCrashReportDetails() ? errormessage : NULL);
|
ShouldServeCrashReportDetails() ? lua_tostring(L, -1) : NULL);
|
||||||
|
lua_pop(L, 1); // pop error
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error = ServeErrorWithDetail(
|
error = ServeErrorWithDetail(
|
||||||
500, "Internal Server Error",
|
500, "Internal Server Error",
|
||||||
ShouldServeCrashReportDetails() ? errormessage : NULL);
|
ShouldServeCrashReportDetails() ? lua_tostring(L, -1) : NULL);
|
||||||
|
|
||||||
lua_pop(L, 1); // pop error
|
lua_pop(L, 1); // pop error
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue