Optimized uvl.errors
[project/luci.git] / libs / uvl / luasrc / uvl / errors.lua
index 1f6060e..bdd596c 100644 (file)
@@ -14,11 +14,19 @@ $Id$
 
 ]]--
 
-module( "luci.uvl.errors", package.seeall )
+local uci = require "luci.model.uci"
+local uvl = require "luci.uvl"
+local util = require "luci.util"
+local string = require "string"
 
-require("luci.util")
+local ipairs, error, type = ipairs, error, type 
+local tonumber, unpack = tonumber, unpack
 
 
+local luci = luci
+
+module "luci.uvl.errors"
+
 ERRCODES = {
        { 'UCILOAD',            'Unable to load config "%p": %1' },
 
@@ -26,7 +34,7 @@ ERRCODES = {
        { 'CONFIG',             'Error in config "%p":\n%c' },
        { 'SECTION',            'Error in section "%i" (%I):\n%c' },
        { 'OPTION',                     'Error in option "%i" (%I):\n%c' },
-       { 'REFERENCE',          'Option "%p.%s.%o" has invalid reference specification %1:\n%c' },
+       { 'REFERENCE',          'Option "%i" has invalid reference specification %1:\n%c' },
        { 'DEPENDENCY',         'In dependency check for %t "%i":\n%c' },
 
        { 'SME_FIND',           'Can not find scheme "%p" in "%1"' },
@@ -42,8 +50,8 @@ ERRCODES = {
        { 'SME_EBADPACK',       'Enum "%v" in scheme "%p" references unknown package "%1"' },
        { 'SME_EBADSECT',       'Enum "%v" in scheme "%p" references unknown section "%1"' },
        { 'SME_EBADOPT',        'Enum "%v" in scheme "%p" references unknown option "%1"'  },
-       { 'SME_EBADTYPE',       'Enum "%v" in scheme "%p" references non-enum option "%p.%s.%o"' },
-       { 'SME_EBADDEF',        'Enum "%v" in scheme "%p" redeclares the default value of "%p.%s.%o"' },
+       { 'SME_EBADTYPE',       'Enum "%v" in scheme "%p" references non-enum option "%I"' },
+       { 'SME_EBADDEF',        'Enum "%v" in scheme "%p" redeclares the default value of "%I"' },
 
        { 'SECT_UNKNOWN',       'Section "%i" (%I) not found in scheme' },
        { 'SECT_REQUIRED',      'Required section "%p.%S" not found in config' },
@@ -53,8 +61,8 @@ ERRCODES = {
 
        { 'OPT_UNKNOWN',        'Option "%i" (%I) not found in scheme' },
        { 'OPT_REQUIRED',       'Required option "%i" has no value' },
-       { 'OPT_BADVALUE',       'Value "%v" of option "%i" is not defined in enum %1' },
-       { 'OPT_INVVALUE',       'Value "%v" of given option "%i" does not validate as datatype "%1"' },
+       { 'OPT_BADVALUE',       'Value "%1" of option "%i" is not defined in enum %2' },
+       { 'OPT_INVVALUE',       'Value "%1" of option "%i" does not validate as datatype "%2"' },
        { 'OPT_NOTLIST',        'Option "%i" is defined as list but stored as plain value' },
        { 'OPT_DATATYPE',       'Option "%i" has unknown datatype "%1"' },
        { 'OPT_NOTFOUND',       'Option "%p.%s.%o" not found in config' },
@@ -68,11 +76,11 @@ ERRCODES = {
 
 -- build error constants and instance constructors
 for i, v in ipairs(ERRCODES) do
-       luci.uvl.errors[v[1]] = function(...)
+       _M[v[1]] = function(...)
                return error(i, ...)
        end
 
-       luci.uvl.errors['ERR_'..v[1]] = i
+       _M['ERR_'..v[1]] = i
 end
 
 
@@ -85,14 +93,14 @@ function i18n(key, def)
 end
 
 
-error = luci.util.class()
+error = util.class()
 
 function error.__init__(self, code, pso, args)
 
        self.code = code
        self.args = ( type(args) == "table" and args or { args } )
 
-       if luci.util.instanceof( pso, luci.uvl.uvlitem ) then
+       if util.instanceof( pso, uvl.uvlitem ) then
                self.stype = pso.sref[2]
                self.package, self.section, self.option, self.value = unpack(pso.cref)
                self.object = pso
@@ -101,7 +109,7 @@ function error.__init__(self, code, pso, args)
                pso = ( type(pso) == "table" and pso or { pso } )
 
                if pso[2] then
-                       local uci = luci.model.uci.cursor()
+                       local uci = uci.cursor()
                        self.stype = uci:get(pso[1], pso[2]) or pso[2]
                end
 
@@ -128,14 +136,14 @@ function error.string(self,pad)
                :gsub("\n", "\n"..pad)
                :gsub("%%i", self:cid())
                :gsub("%%I", self:sid())
-               :gsub("%%p", self.package or '<nil>')
-               :gsub("%%s", self.section or '<nil>')
-               :gsub("%%S", self.stype   or '<nil>')
-               :gsub("%%o", self.option  or '<nil>')
-               :gsub("%%v", self.value   or '<nil>')
-               :gsub("%%t", self.object and self.object:type()  or '<nil>' )
-               :gsub("%%T", self.object and self.object:title() or '<nil>' )
-               :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '<nil>' end)
+               :gsub("%%p", self.package or '(nil)')
+               :gsub("%%s", self.section or '(nil)')
+               :gsub("%%S", self.stype   or '(nil)')
+               :gsub("%%o", self.option  or '(nil)')
+               :gsub("%%v", self.value   or '(nil)')
+               :gsub("%%t", self.object and self.object:type()  or '(nil)' )
+               :gsub("%%T", self.object and self.object:title() or '(nil)' )
+               :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '(nil)' end)
                :gsub("%%c",
                        function()
                                local s = ""