local string_dist = require 'string_distance' assert(debug and debug.getinfo) -------------------------------------------------------------------------------- -- Error handler local ErrorHandler = setmetatable({}, {__call = function (c, ...) return c.new(...) end}) ErrorHandler.__index = ErrorHandler function ErrorHandler.new (module_name) return setmetatable({module_name = module_name, registered = {}}, ErrorHandler) end function ErrorHandler:register (f) assert(type(self) == 'table') assert(type(f) == 'function') self.registered[f] = true end local ERROR_MODES = { ['internal'] = true, ['external'] = true } function ErrorHandler:getErrorFunc (mode, name) -- Parameter fixing local mode = mode or 'internal' local name = name or (mode == 'external' and '') or mode if name ~= '' then name = '/'..name end -- Error checking assert(ERROR_MODES[mode]) assert(type(name) == 'string') -- Create error funcs. if mode == 'internal' then return function (format_msg, ...) error(('[%s%s]: '..format_msg):format(self.module_name, name, ...), 2) end elseif mode == 'external' then return function (format_msg, ...) local level = 2 while self.registered[debug.getinfo(level, 'f').func] do level = level + 1 end error(('[%s%s]: '..format_msg):format(self.module_name, name, ...), level) end end assert(false) end -------------------------------------------------------------------------------- return { ErrorHandler = ErrorHandler }