* luci/libs/uvl: fix option dependency error handling in case of different non-critic...
[project/luci.git] / libs / uvl / luasrc / uvl / errors.lua
index c53a4ab..a575f40 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' },
 
@@ -54,10 +62,11 @@ ERRCODES = {
        { '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 "%v" of given option "%i" does not validate as datatype "%1"' },
+       { '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' },
@@ -68,11 +77,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 +94,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 +110,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 +122,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
@@ -175,3 +184,17 @@ 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
+                       equal = util.contains(codes, c.code)
+               end
+               return equal
+       end
+end