* luci/libs: uvl: use python-style format syntax for errors

This commit is contained in:
Jo-Philipp Wich 2008-08-24 17:12:16 +00:00
parent 0647fadb0a
commit 5117fc703e
2 changed files with 44 additions and 45 deletions

View file

@ -122,12 +122,9 @@ function UVL.get_dependencies( self, config, section, option )
if r then if r then
sdeps[r] = v sdeps[r] = v
else else
return nil, string.format( return nil,
'Ambiguous dependency reference "%s" for object ' .. 'Ambiguous dependency reference "%s" for object "%s" given'
'"%s.%s%s" given', %{ k, self.log.id( config.name, section, option ) }
k, config.name, section,
option and '.' .. option or ''
)
end end
end end
table.insert( deps, sdeps ) table.insert( deps, sdeps )
@ -172,7 +169,7 @@ function UVL.validate_config( self, config )
local sc = { } local sc = { }
if not co then if not co then
return false, 'Unable to load configuration "' .. config .. '"' return false, 'Unable to load configuration "%s"' % config
end end
local function _uci_foreach( type, func ) local function _uci_foreach( type, func )
@ -205,8 +202,8 @@ function UVL.validate_config( self, config )
for k, v in pairs(co) do for k, v in pairs(co) do
if not self.beenthere[config..'.'..k] then if not self.beenthere[config..'.'..k] then
return false, self.log.config_error( config, return false, self.log.config_error( config,
"Section '" .. config .. '.' .. co[k]['.type'] .. 'Section "%s" not found in scheme'
"' not found in scheme" ) % self.log.id( config, co[k]['.type'] ) )
end end
end end
end end
@ -216,10 +213,10 @@ function UVL.validate_config( self, config )
if s.required and sc[k] == 0 then if s.required and sc[k] == 0 then
return false, self.log.config_error( config, return false, self.log.config_error( config,
'Required section "' .. k .. '" not found in config' ) 'Required section "%s" not found in config' % k )
elseif s.unique and sc[k] > 1 then elseif s.unique and sc[k] > 1 then
return false, self.log.config_error( config, return false, self.log.config_error( config,
'Unique section "' .. k .. '" occurs multiple times in config' ) 'Unique section "%s" occurs multiple times in config' % k )
end end
end end
@ -246,7 +243,7 @@ function UVL.validate_section( self, config, section )
local co = self.uci.get_all( config ) local co = self.uci.get_all( config )
if not co then if not co then
return false, 'Unable to load configuration "' .. config .. '"' return false, 'Unable to load configuration "%s"' % config
end end
if co[section] then if co[section] then
@ -254,8 +251,8 @@ function UVL.validate_section( self, config, section )
self, co, co[section]['.type'], config, section self, co, co[section]['.type'], config, section
) ) ) )
else else
return false, 'Section "' .. config .. '.' .. section .. return false, 'Section "%s" not found in config. Nothing to do.'
'" not found in config. Nothing to do.' % self.log.id( config, section )
end end
end end
@ -280,7 +277,7 @@ function UVL.validate_option( self, config, section, option )
local co = self.uci.get_all( config ) local co = self.uci.get_all( config )
if not co then if not co then
return false, 'Unable to load configuration "' .. config .. '"' return false, 'Unable to load configuration "%s"' % config
end end
if co[section] and co[section][option] then if co[section] and co[section][option] then
@ -288,9 +285,8 @@ function UVL.validate_option( self, config, section, option )
self, co, co[section]['.type'], config, section, option self, co, co[section]['.type'], config, section, option
) ) ) )
else else
return false, 'Option "' .. return false, 'Option "%s" not found in config. Nothing to do.'
config .. '.' .. section .. '.' .. option .. % self.log.id( config, section, option )
'" not found in config. Nothing to do.'
end end
end end
@ -302,8 +298,8 @@ function UVL._validate_section( self, section )
section:values()['.anonymous'] == true section:values()['.anonymous'] == true
then then
return false, self.log.section_error( section, return false, self.log.section_error( section,
'The section of type "' .. section:sid() .. '" is stored ' .. 'The section of type "%s" is stored anonymously in config but must be named'
'anonymously in config but must be named' ) % section:sid() )
end end
for _, v in ipairs(section:variables()) do for _, v in ipairs(section:variables()) do
@ -320,7 +316,7 @@ function UVL._validate_section( self, section )
return false, err return false, err
end end
else else
return false, 'Option "' .. section:sid() .. '" not found in config' return false, 'Option "%s" not found in config' % section:sid()
end end
if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then if STRICT_UNKNOWN_OPTIONS and not section:section().dynamic then
@ -328,8 +324,8 @@ function UVL._validate_section( self, section )
if k:sub(1,1) ~= "." and not self.beenthere[ if k:sub(1,1) ~= "." and not self.beenthere[
section:cid() .. '.' .. k section:cid() .. '.' .. k
] then ] then
return false, "Option '" .. section:sid() .. '.' .. k .. return false, 'Option "%s" not found in scheme'
"' not found in scheme" % self.log.id( unpack(section:sid()), k )
end end
end end
end end
@ -343,27 +339,26 @@ function UVL._validate_option( self, option, nodeps )
local val = option:value() local val = option:value()
if not item and not ( option:section() and option:section().dynamic ) then if not item and not ( option:section() and option:section().dynamic ) then
return false, 'Option "' .. option:cid() .. return false, 'Option "%s" not found in scheme' % option:cid()
'" not found in scheme'
elseif item then elseif item then
if item.required and not val then if item.required and not val then
return false, 'Mandatory variable "' .. option:cid() .. return false, 'Mandatory variable "%s" does not have a value'
'" does not have a value' % option:cid()
end end
if item.type == "enum" and val then if item.type == "enum" and val then
if not item.values or not item.values[val] then if not item.values or not item.values[val] then
return false, 'Value "' .. ( val or '<nil>' ) .. return false,
'" of given option "' .. option:cid() .. 'Value "%s" of given option "%s" is not defined in enum { %s }'
'" is not defined in enum { ' .. %{ val or '<nil>', option:cid(),
table.concat( luci.util.keys(item.values), ", " ) .. table.concat( luci.util.keys(item.values), ", " ) }
' }'
end end
elseif item.type == "list" and val then elseif item.type == "list" and val then
if type(val) ~= "table" and STRICT_LIST_TYPE then if type(val) ~= "table" and STRICT_LIST_TYPE then
return false, 'Option "' .. option:cid() .. return false,
'" is defined as list but stored as plain value' 'Option "%s" is defined as list but stored as plain value'
% option:cid()
end end
end end
@ -372,16 +367,15 @@ function UVL._validate_option( self, option, nodeps )
val = ( type(val) == "table" and val or { val } ) val = ( type(val) == "table" and val or { val } )
for i, v in ipairs(val) do for i, v in ipairs(val) do
if not self.datatypes[item.datatype]( v ) then if not self.datatypes[item.datatype]( v ) then
return false, 'Value' .. ( #val>1 and ' #'..i or '' ) .. return false,
' "' .. ( v or '<nil>' ) .. 'Value%s "%s" of given option "%s" does not validate as datatype "%s"'
'" of given option "' .. option:cid() .. %{ ( #val>1 and ' #' .. i or '' ), v,
'" does not validate as datatype "' .. option:cid(), item.datatype }
item.datatype .. '"'
end end
end end
else else
return false, 'Unknown datatype "' .. return false, 'Unknown datatype "%s" encountered'
item.datatype .. '" encountered' % item.datatype
end end
end end
@ -418,10 +412,7 @@ function UVL.read_scheme( self, scheme )
return self:_read_scheme_parts( scheme, schemes ) return self:_read_scheme_parts( scheme, schemes )
else else
error( error( 'Can not find scheme "%s" in "%s"' %{ scheme, self.schemedir } )
'Can not find scheme "' .. scheme ..
'" in "' .. self.schemedir .. '"'
)
end end
end end

View file

@ -71,3 +71,11 @@ function dump_dependency( dep, ref, v, e )
return str return str
end end
function id( c, s, o )
if type(c) == "table" then
c, s, o = unpack(c)
end
return c .. ( s and '.' .. s or '' ) .. ( o and '.' .. o or '' )
end