mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-07 02:10:27 +00:00
Refactor code => handle all errors
This commit is contained in:
parent
e3e37f77c8
commit
42003209cb
1 changed files with 28 additions and 26 deletions
|
@ -2533,7 +2533,7 @@ img { vertical-align: middle; }\r\n\
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *ServeErrorImpl(unsigned code, const char *reason,
|
static char *ServeErrorImplDefault(unsigned code, const char *reason,
|
||||||
const char *details) {
|
const char *details) {
|
||||||
size_t n;
|
size_t n;
|
||||||
char *p, *s;
|
char *p, *s;
|
||||||
|
@ -2571,6 +2571,28 @@ static char *ServeErrorImpl(unsigned code, const char *reason,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *GetLuaResponse(void) {
|
||||||
|
return cpm.luaheaderp ? cpm.luaheaderp : SetStatus(200, "OK");
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ServeErrorImpl(unsigned code, const char *reason,
|
||||||
|
const char *details) {
|
||||||
|
lua_State *L = GL;
|
||||||
|
if (hasonerror) {
|
||||||
|
lua_getglobal(L, "OnError");
|
||||||
|
lua_pushinteger(L, code);
|
||||||
|
lua_pushstring(L, reason);
|
||||||
|
if (LuaCallWithTrace(L, 2, 0, NULL) == LUA_OK) {
|
||||||
|
return CommitOutput(GetLuaResponse());
|
||||||
|
} else {
|
||||||
|
return ServeErrorImplDefault(code, reason, details);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return ServeErrorImplDefault(code, reason, details);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static char *ServeErrorWithPath(unsigned code, const char *reason,
|
static char *ServeErrorWithPath(unsigned code, const char *reason,
|
||||||
const char *path, size_t pathlen) {
|
const char *path, size_t pathlen) {
|
||||||
ERRORF("(srvr) server error: %d %s %`'.*s", code, reason, pathlen, path);
|
ERRORF("(srvr) server error: %d %s %`'.*s", code, reason, pathlen, path);
|
||||||
|
@ -3228,10 +3250,6 @@ static char *ServeIndex(const char *path, size_t pathlen) {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *GetLuaResponse(void) {
|
|
||||||
return cpm.luaheaderp ? cpm.luaheaderp : SetStatus(200, "OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool ShouldServeCrashReportDetails(void) {
|
static bool ShouldServeCrashReportDetails(void) {
|
||||||
uint32_t ip;
|
uint32_t ip;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
|
@ -3253,27 +3271,11 @@ static char *LuaOnHttpRequest(void) {
|
||||||
return CommitOutput(GetLuaResponse());
|
return CommitOutput(GetLuaResponse());
|
||||||
} else {
|
} else {
|
||||||
LogLuaError("OnHttpRequest", lua_tostring(L, -1));
|
LogLuaError("OnHttpRequest", lua_tostring(L, -1));
|
||||||
|
error = ServeErrorWithDetail(
|
||||||
if (hasonerror) {
|
500, "Internal Server Error",
|
||||||
lua_getglobal(L, "OnError");
|
ShouldServeCrashReportDetails() ? lua_tostring(L, -1) : NULL);
|
||||||
lua_pushinteger(L, 500);
|
lua_pop(L, 1); // pop error
|
||||||
lua_pushstring(L, lua_tostring(L, -1));
|
return error;
|
||||||
if (LuaCallWithTrace(L, 2, 0, NULL) == LUA_OK) {
|
|
||||||
return CommitOutput(GetLuaResponse());
|
|
||||||
} else {
|
|
||||||
error = ServeErrorWithDetail(
|
|
||||||
500, "Internal Server Error!!",
|
|
||||||
ShouldServeCrashReportDetails() ? lua_tostring(L, -1) : NULL);
|
|
||||||
lua_pop(L, 1); // pop error
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error = ServeErrorWithDetail(
|
|
||||||
500, "Internal Server Error",
|
|
||||||
ShouldServeCrashReportDetails() ? lua_tostring(L, -1) : NULL);
|
|
||||||
lua_pop(L, 1); // pop error
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue