X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=libs%2Fuvl%2Fluasrc%2Fuvl%2Ferrors.lua;h=6d0b02c1e18cd8169ac530ab59e24c251cf2a488;hb=95bc270f063d106f578246ea8d1b3dc6509ad2f0;hp=c547030a82a554d8af0eac60308056d37094372e;hpb=4cfd1d25d16aaac1f2e0556a7cc589f0e5200669;p=project%2Fluci.git diff --git a/libs/uvl/luasrc/uvl/errors.lua b/libs/uvl/luasrc/uvl/errors.lua index c547030a82..6d0b02c1e1 100644 --- a/libs/uvl/luasrc/uvl/errors.lua +++ b/libs/uvl/luasrc/uvl/errors.lua @@ -14,67 +14,66 @@ $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 -ERRCODES = { - { 'UCILOAD', 'Unable to load config "%p": %1' }, - - { 'SCHEME', 'Error in scheme "%p":\n%c' }, - { '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 "%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"' }, - { 'SME_READ', 'Can not access file "%1"' }, - { 'SME_REQFLD', 'Missing required scheme field "%1" in "%i"' }, - { 'SME_INVREF', 'Illegal reference "%1" to an anonymous section' }, - { 'SME_BADREF', 'Malformed reference in "%1"' }, - { 'SME_BADDEP', 'Malformed dependency specification "%1" in "%i"' }, - { 'SME_BADVAL', 'Malformed validator specification "%1" in "%i"' }, - { 'SME_ERRVAL', 'External validator "%1" failed: %2' }, - { 'SME_VBADPACK', 'Variable "%o" in scheme "%p" references unknown package "%1"' }, - { 'SME_VBADSECT', 'Variable "%o" in scheme "%p" references unknown section "%1"' }, - { '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 "%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' }, - { 'SECT_UNIQUE', 'Unique section "%p.%S" occurs multiple times in config' }, - { 'SECT_NAMED', 'The section of type "%p.%S" is stored anonymously in config but must be named' }, - { 'SECT_NOTFOUND', 'Section "%p.%s" not found in config' }, - - { '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_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' }, - - { 'DEP_NOTEQUAL', 'Dependency (%1) failed:\nOption "%i" is not eqal "%2"' }, - { 'DEP_NOVALUE', 'Dependency (%1) failed:\nOption "%i" has no value' }, - { 'DEP_NOTVALID', 'Dependency (%1) failed:\n%c' }, - { 'DEP_RECURSIVE', 'Recursive dependency for option "%i" detected' }, - { 'DEP_BADENUM', 'In dependency check for enum value "%i":\n%c' } -} - --- build error constants and instance constructors -for i, v in ipairs(ERRCODES) do - luci.uvl.errors[v[1]] = function(...) - return error(i, ...) - end +local luci = luci - luci.uvl.errors['ERR_'..v[1]] = i -end +module "luci.uvl.errors" +ERRCODES = { + UCILOAD = 'Unable to load config "%p": %1', + + SCHEME = 'Error in scheme "%p":\n%c', + 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 "%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"', + SME_READ = 'Can not access file "%1"', + SME_REQFLD = 'Missing required scheme field "%1" in "%i"', + SME_INVREF = 'Illegal reference "%1" to an anonymous section', + SME_BADREF = 'Malformed reference in "%1"', + SME_BADDEP = 'Malformed dependency specification "%1" in "%i"', + SME_BADVAL = 'Malformed validator specification "%1" in "%i"', + SME_ERRVAL = 'External validator "%1" failed: %2', + SME_VBADPACK = 'Variable "%o" in scheme "%p" references unknown package "%1"', + SME_VBADSECT = 'Variable "%o" in scheme "%p" references unknown section "%1"', + 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 "%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', + SECT_UNIQUE = 'Unique section "%p.%S" occurs multiple times in config', + SECT_NAMED = 'The section of type "%p.%S" is stored anonymously in config but must be named', + SECT_NOTFOUND = 'Section "%p.%s" not found in config', + + OPT_UNKNOWN = 'Option "%i" (%I) not found in scheme', + OPT_REQUIRED = 'Required option "%i" has no value', + 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', + OPT_RANGE = 'Option "%p.%s.%o" is not within the specified range', + + DEP_NOTEQUAL = 'Dependency (%1) failed:\nOption "%i" is not eqal "%2"', + DEP_NOVALUE = 'Dependency (%1) failed:\nOption "%i" has no value', + DEP_NOTVALID = 'Dependency (%1) failed:\n%c', + DEP_RECURSIVE = 'Recursive dependency for option "%i" detected', + DEP_BADENUM = 'In dependency check for enum value "%i":\n%c' +} function i18n(key, def) if luci.i18n then @@ -85,14 +84,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 +100,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 @@ -113,7 +112,7 @@ function error.child(self, err) if not self.childs then self.childs = { err } else - table.insert( self.childs, err ) + self.childs[#self.childs+1] = err end return self end @@ -122,20 +121,20 @@ function error.string(self,pad) pad = pad or " " local str = i18n( - 'uvl_err_%s' % string.lower(ERRCODES[self.code][1]), - ERRCODES[self.code][2] + 'uvl_err_%s' % string.lower(self.code), + ERRCODES[self.code] ) :gsub("\n", "\n"..pad) :gsub("%%i", self:cid()) :gsub("%%I", self:sid()) - :gsub("%%p", self.package or '') - :gsub("%%s", self.section or '') - :gsub("%%S", self.stype or '') - :gsub("%%o", self.option or '') - :gsub("%%v", self.value or '') - :gsub("%%t", self.object and self.object:type() or '' ) - :gsub("%%T", self.object and self.object:title() or '' ) - :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '' 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 = "" @@ -175,3 +174,21 @@ function error.is(self, code) end return false end + +function error.is_all(self, ...) + local codes = { ... } + + if util.contains(codes, self.code) then + return true + else + local equal = false + for _, c in ipairs(self.childs) do + if c.childs then + equal = c:is_all(...) + else + equal = util.contains(codes, c.code) + end + end + return equal + end +end