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,
__newindex = function(self,key,value)
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(
"Tried to create a global %q with value %s\n%s",
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 = {
route = "/_login",
name = "login",
@ -194,6 +204,7 @@ local smr_mock_env = {
}
local sfmt = string.format
local string_fmt_override = {
--[[
format = spy.new(function(fmt,...)
local args = {...}
for i = 1,#args do
@ -204,19 +215,20 @@ local string_fmt_override = {
table.insert(args,1,fmt)
return sfmt(unpack(args))
end)
]]
}
setmetatable(string_fmt_override,{__index = string})
local smr_override_env = {
--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
string = string_fmt_override
--string = string_fmt_override
}
local smr_mock_env_m = {
__index = smr_mock_env,
__newindex = function(self,key,value)
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(
"Tried to create a global %q with value %s\n%s",
key,

View File

@ -45,10 +45,10 @@ local function bio_edit_get(req)
errcode = 500,
errcodemsg = "Server error",
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.
]], author, authorid),
should_traceback=true
]], tostring(author), tostring(authorid)),
should_traceback = true
}
stmnt_bio:reset()
http_response(req,500,ret)

View File

@ -24,6 +24,16 @@ local function edit_bio(req)
local host = http_request_get_host(req)
local path = http_request_get_path(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)
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)
if db.do_sql(stmnt_update_bio) ~= sql.DONE then
stmnt_update_bio:reset()
error("Faled to update biography")
error("Failed to update biography")
end
stmnt_update_bio:reset()
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()
stmnt_author_of:reset()
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 compr_raw = zlib.compress(text)
local compr = zlib.compress(parsed)

View File

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