Add some better error reporting

This commit is contained in:
Robin Malley 2023-03-12 05:13:54 +00:00
parent 76c462cb60
commit 5c5637ff40
6 changed files with 52 additions and 18 deletions

View File

@ -127,7 +127,7 @@ local smr_mock_env_m = {
__index = smr_mock_env, __index = smr_mock_env,
__newindex = function(self,key,value) __newindex = function(self,key,value)
local setter = debug.getinfo(2) local setter = debug.getinfo(2)
if setter.source ~= "=[C]" and key ~= "configure" then if setter.source ~= "=[C]" and setter.source ~= "@./global.lua" and key ~= "configure" then
error(string.format( error(string.format(
"Tried to create a global %q with value %s\n%s", "Tried to create a global %q with value %s\n%s",
key, key,

View File

@ -79,7 +79,17 @@ local pages = {
}, },
} }
}, },
--TODO:bio bio = {
route = "/_bio",
name = "edit_bio",
methods = {
GET={},
POST={
user = rng_subdomain,
pass = rng_any
},
}
},
login = { login = {
route = "/_login", route = "/_login",
name = "login", name = "login",
@ -194,6 +204,7 @@ local smr_mock_env = {
} }
local sfmt = string.format local sfmt = string.format
local string_fmt_override = { local string_fmt_override = {
--[[
format = spy.new(function(fmt,...) format = spy.new(function(fmt,...)
local args = {...} local args = {...}
for i = 1,#args do for i = 1,#args do
@ -204,19 +215,20 @@ local string_fmt_override = {
table.insert(args,1,fmt) table.insert(args,1,fmt)
return sfmt(unpack(args)) return sfmt(unpack(args))
end) end)
]]
} }
setmetatable(string_fmt_override,{__index = string}) setmetatable(string_fmt_override,{__index = string})
local smr_override_env = { local smr_override_env = {
--Detour assert so we don't actually perform any checks --Detour assert so we don't actually perform any checks
assert = spy.new(function(bool,msg,level) return bool end), --assert = spy.new(function(bool,msg,level) return bool end),
--Allow string.format to accept nil as arguments --Allow string.format to accept nil as arguments
string = string_fmt_override --string = string_fmt_override
} }
local smr_mock_env_m = { local smr_mock_env_m = {
__index = smr_mock_env, __index = smr_mock_env,
__newindex = function(self,key,value) __newindex = function(self,key,value)
local setter = debug.getinfo(2) local setter = debug.getinfo(2)
if setter.source ~= "=[C]" and key ~= "configure" then if setter.source ~= "=[C]" and setter.source ~= "@./global.lua" and key ~= "configure" then
error(string.format( error(string.format(
"Tried to create a global %q with value %s\n%s", "Tried to create a global %q with value %s\n%s",
key, key,

View File

@ -45,10 +45,10 @@ local function bio_edit_get(req)
errcode = 500, errcode = 500,
errcodemsg = "Server error", errcodemsg = "Server error",
explanation = string.format([[ explanation = string.format([[
Tried to get the biography of author %q (%d) but no author with that id was Tried to get the biography of author %q (%s) but no author with that id was
found, please report this error. found, please report this error.
]], author, authorid), ]], tostring(author), tostring(authorid)),
should_traceback=true should_traceback = true
} }
stmnt_bio:reset() stmnt_bio:reset()
http_response(req,500,ret) http_response(req,500,ret)

View File

@ -24,6 +24,16 @@ local function edit_bio(req)
local host = http_request_get_host(req) local host = http_request_get_host(req)
local path = http_request_get_path(req) local path = http_request_get_path(req)
local author, author_id = session.get(req) local author, author_id = session.get(req)
if not (author and author_id) then
local response = pages.error{
errcode = 401,
errcodemsg = "Unauthorized",
explanation = string.format("You must be logged in to edit a biography. Your login session may have expiried."),
should_traceback = true,
}
http_response(req,401,response)
return
end
http_request_populate_post(req) http_request_populate_post(req)
local text = http_argument_get_string(req,"text") or "" local text = http_argument_get_string(req,"text") or ""
@ -36,7 +46,7 @@ local function edit_bio(req)
db.sqlbind(stmnt_update_bio, "bind", 2, author_id) db.sqlbind(stmnt_update_bio, "bind", 2, author_id)
if db.do_sql(stmnt_update_bio) ~= sql.DONE then if db.do_sql(stmnt_update_bio) ~= sql.DONE then
stmnt_update_bio:reset() stmnt_update_bio:reset()
error("Faled to update biography") error("Failed to update biography")
end end
stmnt_update_bio:reset() stmnt_update_bio:reset()
local loc = string.format("https://%s.%s",author,config.domain) local loc = string.format("https://%s.%s",author,config.domain)

View File

@ -55,7 +55,16 @@ local function edit_post(req)
local data = stmnt_author_of:get_values() local data = stmnt_author_of:get_values()
stmnt_author_of:reset() stmnt_author_of:reset()
local realauthor = data[1] local realauthor = data[1]
assert(realauthor == author_id) --Make sure the author of the story is the currently logged in user if realauthor ~= author_id then
local response = pages.error{
errcode = 401,
errcodemsg = "Unauthorized",
explanation = string.format("You are trying to edit post %d, but it is another user's post. You are %s.",storyid, author_id),
should_traceback = true,
}
http_response(req,401,response)
return
end
local parsed = parsers[markup](text) local parsed = parsers[markup](text)
local compr_raw = zlib.compress(text) local compr_raw = zlib.compress(text)
local compr = zlib.compress(parsed) local compr = zlib.compress(parsed)

View File

@ -27,20 +27,23 @@ for k,v in pairs(pagenames) do
local parser = et.Parser() local parser = et.Parser()
local f = assert(io.open(path,"r")) local f = assert(io.open(path,"r"))
local fdata = assert(f:read("*a")) local fdata = assert(f:read("*a"))
local e,code = assert(xpcall(function() local code, err = parser:compile_to_lua(fdata)
return parser:compile_to_lua(fdata) if not code then
end,function(err) errorf("Failed to parse %s: %s",path,err)
return debug.traceback(string.format("Failed to parse %s: %s",path, err)) end
end))
local func, err = parser:load(code) local func, err = parser:load(code)
if not func then if not func then
error(string.format("Failed to load %s: %s",path, err)) error(string.format("Failed to load %s: %s",path, err))
end end
f:close() f:close()
assert(func, "Failed to load " .. path) assert(func, "Failed to load " .. path)
pages[v] = function(...) pages[v] = function(env)
local buf = assert(parser:run(func,...)) assert(type(env) == "table","env must be a table")
return table.concat(buf) local buff, err = parser:run(func,env)
if not buff then
errorf("Failed to render %s : %s", path, err)
end
return table.concat(buff)
end end
end end