1
0
errors/errors.lua

193 lines
6.8 KiB
Lua
Raw Normal View History

2017-06-27 11:50:27 +00:00
-- Check for possible loading errors
local string_dist
do
2017-06-27 11:50:27 +00:00
local thispath = ... and (...):match('.+%.') or ''
local function import (name, ignore_failure)
local was_loaded, lib_or_error = pcall(require, thispath..name)
if not was_loaded then
if ignore_failure then return nil end
error('['..thispath..']: Could not load vital library: '..name..'.lua:\n\t'..lib_or_error)
end
return lib_or_error
end
string_dist = import 'string_distance'
end
2017-06-27 11:50:27 +00:00
if not debug then
error('[errors/loading]: Expected the debug library to be available in _G.')
elseif not debug.getinfo then
error('[errors/loading]: Expected debug.getinfo to be available in the debug library.')
end
--------------------------------------------------------------------------------
-- Constants
2017-06-27 11:50:27 +00:00
local DEFAULT_ERROR_MSG_NORMAL = 'Unknown error occured'
local DEFAULT_ERROR_MSG_INTERNAL = 'Unknown internal error occured'
local DEFAULT_ERROR_MSG_CORRECT = 'Unexpected input "%s", maybe you meant %s?'
--------------------------------------------------------------------------------
-- Util
local function format_probable_strings (probable_strings, amount)
assert(type(probable_strings) == 'table')
assert(#probable_strings > 0)
2017-06-27 11:50:27 +00:00
assert(type(amount) == 'number')
local l = {}
for i = 1, math.min(amount, #probable_strings) do
l[#l+1] = probable_strings[i]
l[#l+1] = ', '
end
if l[#l] == ', ' then l[#l] = nil end
if #l >= 3 then l[#l-1] = ' or ' end
return table.concat(l, '')
end
--------------------------------------------------------------------------------
2017-06-27 11:50:27 +00:00
local function internal_error (self, module_suffix, format_msg, ...)
-- internal_error(error_handler, module_suffix [, format_msg, ...])
2017-06-27 11:50:27 +00:00
-- Error check
assert(type(self) == 'table' and self.is_error_handler)
assert(type(module_suffix) == 'string')
format_msg = format_msg or DEFAULT_ERROR_MSG_NORMAL
assert(type(format_msg) == 'string')
-- Format
return error(('[%s%s]: '..format_msg):format(self.module_name, module_suffix, ...), 1)
end
2017-06-27 11:50:27 +00:00
local function external_error (self, module_suffix, format_msg, ...)
-- external_error(error_handler, module_suffix [, format_msg, ...])
-- Error check
assert(type(self) == 'table' and self.is_error_handler)
assert(type(module_suffix) == 'string')
format_msg = format_msg or DEFAULT_ERROR_MSG_INTERNAL
assert(type(format_msg) == 'string')
-- Find error level
local level = 3
while self.registered[debug.getinfo(level, 'f').func] do
level = level + 1
end
-- Format
return error(('[%s%s]: '..format_msg):format(self.module_name, module_suffix, ...), 1)
end
2017-06-27 11:50:27 +00:00
local function correct_error (self, module_suffix, format_msg, gotten_string, possible_strings)
-- correct_error(error_handler, module_suffix [, format_msg], gotten_string, possible_strings)
2017-06-27 11:50:27 +00:00
-- Error check
assert(type(self) == 'table' and self.is_error_handler)
assert(type(module_suffix) == 'string')
format_msg = format_msg or DEFAULT_ERROR_MSG_CORRECT
assert(type(format_msg) == 'string')
assert(type(gotten_string) == 'string')
assert(type(possible_strings) == 'table')
2017-06-27 11:50:27 +00:00
-- Do stuff
local possible_strings = string_dist.strings_with_highest_similarity(gotten_string, possible_strings)
local list_string = format_probable_strings(possible_strings, 3)
-- Format
return error(('[%s%s]: '..format_msg):format(self.module_name, module_suffix, gotten_string, list_string), 1)
2017-06-27 11:50:27 +00:00
end
----
local function handle_index_error (err_hdl, t, key)
--
assert(type(err_hdl) == 'table')
assert(type(t) == 'table')
-- Find keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- Is empty table?
if #keys == 0 then
external_error(err_hdl, '', 'Cannot index unknown key %s in empty table.', key)
end
-- Is sequence?
if #keys == #t then
if type(key) ~= 'number' then
external_error(err_hdl, '', 'Cannot index %s %s, for sequence with length %i.', type(key), key, #t)
elseif key == math.floor(key) then -- Integer
external_error(err_hdl, '', 'Cannot index integer %i, for sequence with length %i.', key, #t)
elseif key ~= math.floor(key) then -- Non-Integer
local format_code = (key == tonumber(tostring(key))) and '%s' or '%f'
external_error(err_hdl, '', 'Cannot index non-integer '..format_code..', for sequence with length %i.', key, #t)
end
end
-- Is map?
for i = 1, #keys do keys[i] = tostring(keys[i]) end
correct_error(err_hdl, '', 'Cannot index unknown key %s, maybe you meant %s?', tostring(key), keys)
end
----
2017-06-27 11:50:27 +00:00
local ErrorHandler_mt = {__call = function (self, ...) internal_error(self, '', ...) end}
2017-06-27 11:50:27 +00:00
return function (module_name)
assert(type(module_name) == 'string')
2017-06-27 11:50:27 +00:00
local err_hdl = setmetatable({}, ErrorHandler_mt)
err_hdl.is_error_handler = true
err_hdl.module_name = module_name
err_hdl.registered = {}
err_hdl.any_registered = false
2017-06-27 11:50:27 +00:00
function err_hdl.internal (...)
local args = {...}
if args[1] == err_hdl then table.remove(args, 1) end
internal_error(err_hdl, '/internal', unpack(args))
end
function err_hdl.external (...)
local args = {...}
if args[1] == err_hdl then table.remove(args, 1) end
external_error(err_hdl, '', unpack(args))
end
2017-06-27 11:50:27 +00:00
function err_hdl.correct (...)
local args = {...}
if args[1] == err_hdl then table.remove(args, 1) end
if #args == 2 then table.insert(args, 1, DEFAULT_ERROR_MSG_CORRECT) end
correct_error(err_hdl, '', unpack(args))
end
function err_hdl.info (...)
local args = {...}
if args[1] == err_hdl then table.remove(args, 1) end
2017-09-16 19:19:28 +00:00
return io.write(string.format(unpack(args)))
end
2017-06-27 11:50:27 +00:00
function err_hdl.register (...)
local args = {...}
if args[1] == err_hdl then table.remove(args, 1) end
assert(#args > 0)
for i = 1, #args do
assert(type(args[i]) == 'function')
err_hdl.registered[args[i]] = true
err_hdl.any_registered = true
end
end
function err_hdl.strict_table (...)
local args = {...}
if args[1] == err_hdl then table.remove(args, 1) end
return setmetatable(args[1] or {}, {
__index = function (t, k)
return handle_index_error(err_hdl, t, k)
end
})
end
err_hdl.register(format_probable_strings, internal_error, external_error, correct_error, handle_index_error, err_hdl.internal, err_hdl.external, err_hdl.correct, err_hdl.register, err_hdl.strict_table)
2017-06-27 11:50:27 +00:00
return err_hdl
end