X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fuvl%2Fluasrc%2Fuvl%2Ferrors.lua;h=242965ddc129e2d4fd5b1e0c521f8f00da0a9a69;hb=1fe083b59a0af901300ce454c360acf261fbee80;hp=f3ae4b91468e97c04cf5d0308187ac42fd4069a7;hpb=8be65461bcc64250f4ee1df3eef8de22f2c3f87f;p=project%2Fluci.git diff --git a/libs/uvl/luasrc/uvl/errors.lua b/libs/uvl/luasrc/uvl/errors.lua index f3ae4b914..242965ddc 100644 --- a/libs/uvl/luasrc/uvl/errors.lua +++ b/libs/uvl/luasrc/uvl/errors.lua @@ -14,17 +14,27 @@ $Id$ ]]-- -module( "luci.uvl.errors", package.seeall ) -require("luci.util") +local uci = require "luci.model.uci" +local uvl = require "luci.uvl" +local util = require "luci.util" +local string = require "string" + +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" (syntax error or file not found)' }, + { '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 "%p.%s":\n%c' }, - { 'OPTION', 'Error in option "%p.%s.%o":\n%c' }, - { 'REFERENCE', 'Option "%p.%s.%o" has invalid reference specification "%1":\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"' }, @@ -40,19 +50,19 @@ 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 "%p.%s" not found in scheme' }, + { '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 "%1" not found in scheme' }, + { '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 %t { %2 }' }, - { 'OPT_INVVALUE', 'Value "%1" of given option "%i" does not validate as datatype "%2"' }, + { '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' }, @@ -66,30 +76,40 @@ 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 -error = luci.util.class() +function i18n(key, def) + if luci.i18n then + return luci.i18n.translate(key,def) + else + return def + end +end + + +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 + self.value = self.value or ( pso.value and pso:value() ) else 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 @@ -101,25 +121,29 @@ 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 function error.string(self,pad) pad = pad or " " - local str = ERRCODES[self.code][2] + + local str = i18n( + 'uvl_err_%s' % string.lower(ERRCODES[self.code][1]), + ERRCODES[self.code][2] + ) :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) error(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 = "" @@ -146,3 +170,16 @@ function error.sid(self) ( self.option and '.' .. self.option or '' ) .. ( self.value and '.' .. self.value or '' ) end + +function error.is(self, code) + if self.code == code then + return true + elseif self.childs then + for _, c in ipairs(self.childs) do + if c:is(code) then + return true + end + end + end + return false +end