localize errors

This commit is contained in:
metamuffin 2024-11-10 20:58:55 +01:00
parent b27da71ac0
commit c2c3dcb456
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654
2 changed files with 36 additions and 22 deletions

View file

@ -223,13 +223,13 @@ local function respond(status, title, body)
print("</body></html>") print("</body></html>")
end end
local function error_box(message) local function error_box(message, params)
return string.format([[<div class="notif error"><p>Error: %s</p></div>]], escape(message)) return string.format([[<div class="notif error"><p>Error: %s</p></div>]], escape(format(message, params)))
end end
local function respond_error(message) local function respond_error(message)
respond(400, "Error", function() respond(400, "Error", function()
print(error_box(message)) print(error_box("{!x}", { x = message }))
end) end)
end end
@ -349,25 +349,25 @@ local function r_transaction_post()
end end
end end
if not exists then if not exists then
return error_box("unknown product") return error_box("{+error.unknown_product}")
end end
end end
user_src = user_src or "@Potential" user_src = user_src or "@Potential"
if amount == nil then if amount == nil then
return error_box("amount invalid") return error_box("{+error.invalid_amount}")
end end
if comment == nil or comment:match(matchers_global.comment_opt) == nil then if comment == nil or comment:match(matchers_global.comment_opt) == nil then
return error_box("comment invalid") return error_box("{+error.invalid_comment}")
end end
if user_src == nil or user_src:match(matchers_global.user) == nil then if user_src == nil or user_src:match(matchers_global.user) == nil then
return error_box("source user invalid") return error_box("{+error.invalid_user_src}")
end end
if user_dst == nil or user_dst:match(matchers_global.user) == nil then if user_dst == nil or user_dst:match(matchers_global.user) == nil then
return error_box("destination user invalid") return error_box("{+error.invalid_user_dst}")
end end
local log = io.open("log", "a+") local log = io.open("log", "a+")
if log == nil then if log == nil then
return error_box("failed to open log") return error_box("{+error.open_log}")
end end
local time = os.time() local time = os.time()
log:write(string.format("%d,%s,%s,%d,%s,%s,%s\n", log:write(string.format("%d,%s,%s,%d,%s,%s,%s\n",
@ -393,7 +393,8 @@ local function r_user(username)
return respond(200, string.format("Abrechenbarheit: %s", username), function() return respond(200, string.format("Abrechenbarheit: %s", username), function()
local is_special = username:sub(1, 1) == "@" local is_special = username:sub(1, 1) == "@"
local username_display = username:gsub("@", "") local username_display = username:gsub("@", "")
print(format(is_special and "<h1><i>{!username}</i></h1>" or "<h1>{!username}</h1>", { username = username_display })) print(format(is_special and "<h1><i>{!username}</i></h1>" or "<h1>{!username}</h1>",
{ username = username_display }))
local balance = balances()[username] local balance = balances()[username]
local last_txn = last_txns()[username] local last_txn = last_txns()[username]
local new_user = balance == nil local new_user = balance == nil
@ -563,7 +564,7 @@ end
local function r_create_user() local function r_create_user()
local username = query.create_user local username = query.create_user
if username:match(matchers_global.user) == nil then if username:match(matchers_global.user) == nil then
return respond_error("invalid username " .. username) return respond_error(format("{+error.invalid_user}"))
end end
return redirect(string.format("/%s", urlencode(username))) return redirect(string.format("/%s", urlencode(username)))
end end
@ -572,12 +573,12 @@ local function r_products_post()
local data = form_data() local data = form_data()
local barcode = data.barcode local barcode = data.barcode
if barcode == nil or barcode:match("^[%w_-]*$") == nil then if barcode == nil or barcode:match("^[%w_-]*$") == nil then
return error_box("barcode invalid") return error_box("{+error.invalid_barcode}")
end end
if data.delete then if data.delete then
local new_products = io.open("products.new", "w+") local new_products = io.open("products.new", "w+")
if new_products == nil then if new_products == nil then
return error_box("failed to open new products") return error_box("{+error.open_new_products}")
end end
for a_barcode, price, user, name in read_products() do for a_barcode, price, user, name in read_products() do
if barcode ~= a_barcode then if barcode ~= a_barcode then
@ -592,17 +593,17 @@ local function r_products_post()
local name = data.name local name = data.name
local user = data.user local user = data.user
if price == nil then if price == nil then
return error_box("price invalid") return error_box("{+error.invalid_price}")
end end
if name == nil or name:match(matchers_global.name) == nil then if name == nil or name:match(matchers_global.name) == nil then
return error_box("name invalid") return error_box("{+error.invalid_price}")
end end
if user == nil or user:match(matchers_global.user) == nil then if user == nil or user:match(matchers_global.user) == nil then
return error_box("user invalid") return error_box("{+error.invalid_user}")
end end
local products = io.open("products", "a+") local products = io.open("products", "a+")
if products == nil then if products == nil then
return error_box("failed to open products") return error_box("{+error.open_products}")
end end
products:write(string.format("%s,%d,%s,%s\n", barcode, price, user, name)) products:write(string.format("%s,%d,%s,%s\n", barcode, price, user, name))
products:flush() products:flush()
@ -688,7 +689,7 @@ end
local function extract_username() local function extract_username()
if path == nil then if path == nil then
return respond_error("no path") return respond_error(format("{+error.no_path}"))
end end
local username = urldecode(path:sub(2)) local username = urldecode(path:sub(2))
if username == nil or username:match(matchers_global.user) == nil then if username == nil or username:match(matchers_global.user) == nil then
@ -716,7 +717,7 @@ if path == "/" then
else else
local username = extract_username() local username = extract_username()
if username == nil then if username == nil then
return respond_error("username invalid") return respond_error(format("{+error.invalid_user}"))
elseif query.log then elseif query.log then
return r_log(username) return r_log(username)
else else

View file

@ -53,3 +53,16 @@ user.form.transaction.success=Transaction successful
spus=SPUs spus=SPUs
users=Users users=Users
user.special=This is a special user. user.special=This is a special user.
error.unknown_product=Unknown product
error.invalid_amount=Amount invalid
error.invalid_comment=Comment invalid
error.invalid_user_src=Source user invalid
error.invalid_user_dst=Destination user invalid
error.invalid_user=Username invalid
error.open_log=Failed to open log file
error.invalid_barcode=Barcode invalid
error.open_new_products=Failed to open new products file
error.invalid_price=Price invalid
error.invalid_name=Name invalid
error.open_products=Failed to open products file
error.no_path=No path