-- Check for possible loading errors local string_dist do 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 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 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) 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 -------------------------------------------------------------------------------- local function internal_error (self, module_suffix, format_msg, ...) -- internal_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_NORMAL assert(type(format_msg) == 'string') -- Format return error(('[%s%s]: '..format_msg):format(self.module_name, module_suffix, ...), 3) end 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, ...), level -1) end 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) -- 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') -- 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), 3) end local ErrorHandler_mt = {__call = function (self, ...) internal_error(self, '', ...) end} return function (module_name) assert(type(module_name) == 'string') 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 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 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.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) local keys = {} for k in pairs(t) do keys[#keys+1] = tostring(k) end if #keys == 0 then external_error(err_hdl, '', 'Attempting to index unknown key %s in an empty table.', k) end correct_error(err_hdl, '', 'Attempting to index unknown key %s, maybe you meant %s?', k, keys) end }) end return err_hdl end