3 UCI Validation Layer - Error handling
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 module( "luci.uvl.errors", package.seeall )
23 { 'UCILOAD', 'Unable to load config "%p": %1' },
25 { 'SCHEME', 'Error in scheme "%p":\n%c' },
26 { 'CONFIG', 'Error in config "%p":\n%c' },
27 { 'SECTION', 'Error in section "%i" (%I):\n%c' },
28 { 'OPTION', 'Error in option "%i" (%I):\n%c' },
29 { 'REFERENCE', 'Option "%i" has invalid reference specification %1:\n%c' },
30 { 'DEPENDENCY', 'In dependency check for %t "%i":\n%c' },
32 { 'SME_FIND', 'Can not find scheme "%p" in "%1"' },
33 { 'SME_READ', 'Can not access file "%1"' },
34 { 'SME_REQFLD', 'Missing required scheme field "%1" in "%i"' },
35 { 'SME_INVREF', 'Illegal reference "%1" to an anonymous section' },
36 { 'SME_BADREF', 'Malformed reference in "%1"' },
37 { 'SME_BADDEP', 'Malformed dependency specification "%1" in "%i"' },
38 { 'SME_BADVAL', 'Malformed validator specification "%1" in "%i"' },
39 { 'SME_ERRVAL', 'External validator "%1" failed: %2' },
40 { 'SME_VBADPACK', 'Variable "%o" in scheme "%p" references unknown package "%1"' },
41 { 'SME_VBADSECT', 'Variable "%o" in scheme "%p" references unknown section "%1"' },
42 { 'SME_EBADPACK', 'Enum "%v" in scheme "%p" references unknown package "%1"' },
43 { 'SME_EBADSECT', 'Enum "%v" in scheme "%p" references unknown section "%1"' },
44 { 'SME_EBADOPT', 'Enum "%v" in scheme "%p" references unknown option "%1"' },
45 { 'SME_EBADTYPE', 'Enum "%v" in scheme "%p" references non-enum option "%I"' },
46 { 'SME_EBADDEF', 'Enum "%v" in scheme "%p" redeclares the default value of "%I"' },
48 { 'SECT_UNKNOWN', 'Section "%i" (%I) not found in scheme' },
49 { 'SECT_REQUIRED', 'Required section "%p.%S" not found in config' },
50 { 'SECT_UNIQUE', 'Unique section "%p.%S" occurs multiple times in config' },
51 { 'SECT_NAMED', 'The section of type "%p.%S" is stored anonymously in config but must be named' },
52 { 'SECT_NOTFOUND', 'Section "%p.%s" not found in config' },
54 { 'OPT_UNKNOWN', 'Option "%i" (%I) not found in scheme' },
55 { 'OPT_REQUIRED', 'Required option "%i" has no value' },
56 { 'OPT_BADVALUE', 'Value "%v" of option "%i" is not defined in enum %1' },
57 { 'OPT_INVVALUE', 'Value "%v" of given option "%i" does not validate as datatype "%1"' },
58 { 'OPT_NOTLIST', 'Option "%i" is defined as list but stored as plain value' },
59 { 'OPT_DATATYPE', 'Option "%i" has unknown datatype "%1"' },
60 { 'OPT_NOTFOUND', 'Option "%p.%s.%o" not found in config' },
62 { 'DEP_NOTEQUAL', 'Dependency (%1) failed:\nOption "%i" is not eqal "%2"' },
63 { 'DEP_NOVALUE', 'Dependency (%1) failed:\nOption "%i" has no value' },
64 { 'DEP_NOTVALID', 'Dependency (%1) failed:\n%c' },
65 { 'DEP_RECURSIVE', 'Recursive dependency for option "%i" detected' },
66 { 'DEP_BADENUM', 'In dependency check for enum value "%i":\n%c' }
69 -- build error constants and instance constructors
70 for i, v in ipairs(ERRCODES) do
71 luci.uvl.errors[v[1]] = function(...)
75 luci.uvl.errors['ERR_'..v[1]] = i
79 function i18n(key, def)
81 return luci.i18n.translate(key,def)
88 error = luci.util.class()
90 function error.__init__(self, code, pso, args)
93 self.args = ( type(args) == "table" and args or { args } )
95 if luci.util.instanceof( pso, luci.uvl.uvlitem ) then
96 self.stype = pso.sref[2]
97 self.package, self.section, self.option, self.value = unpack(pso.cref)
99 self.value = self.value or ( pso.value and pso:value() )
101 pso = ( type(pso) == "table" and pso or { pso } )
104 local uci = luci.model.uci.cursor()
105 self.stype = uci:get(pso[1], pso[2]) or pso[2]
108 self.package, self.section, self.option, self.value = unpack(pso)
112 function error.child(self, err)
113 if not self.childs then
114 self.childs = { err }
116 table.insert( self.childs, err )
121 function error.string(self,pad)
125 'uvl_err_%s' % string.lower(ERRCODES[self.code][1]),
126 ERRCODES[self.code][2]
128 :gsub("\n", "\n"..pad)
129 :gsub("%%i", self:cid())
130 :gsub("%%I", self:sid())
131 :gsub("%%p", self.package or '<nil>')
132 :gsub("%%s", self.section or '<nil>')
133 :gsub("%%S", self.stype or '<nil>')
134 :gsub("%%o", self.option or '<nil>')
135 :gsub("%%v", self.value or '<nil>')
136 :gsub("%%t", self.object and self.object:type() or '<nil>' )
137 :gsub("%%T", self.object and self.object:title() or '<nil>' )
138 :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '<nil>' end)
142 for _, err in ipairs(self.childs or {}) do
143 s = s .. err:string(pad.." ") .. "\n" .. pad
149 return (str:gsub("%s+$",""))
152 function error.cid(self)
153 return self.object and self.object:cid() or self.package ..
154 ( self.section and '.' .. self.section or '' ) ..
155 ( self.option and '.' .. self.option or '' ) ..
156 ( self.value and '.' .. self.value or '' )
159 function error.sid(self)
160 return self.object and self.object:sid() or self.package ..
161 ( self.stype and '.' .. self.stype or '' ) ..
162 ( self.option and '.' .. self.option or '' ) ..
163 ( self.value and '.' .. self.value or '' )
166 function error.is(self, code)
167 if self.code == code then
169 elseif self.childs then
170 for _, c in ipairs(self.childs) do