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 )
21 { 'UCILOAD', 'Unable to load config "%p": %1' },
23 { 'SCHEME', 'Error in scheme "%p":\n%c' },
24 { 'CONFIG', 'Error in config "%p":\n%c' },
25 { 'SECTION', 'Error in section "%i" (%I):\n%c' },
26 { 'OPTION', 'Error in option "%i" (%I):\n%c' },
27 { 'REFERENCE', 'Option "%p.%s.%o" has invalid reference specification %1:\n%c' },
28 { 'DEPENDENCY', 'In dependency check for %t "%i":\n%c' },
30 { 'SME_FIND', 'Can not find scheme "%p" in "%1"' },
31 { 'SME_READ', 'Can not access file "%1"' },
32 { 'SME_REQFLD', 'Missing required scheme field "%1" in "%i"' },
33 { 'SME_INVREF', 'Illegal reference "%1" to an anonymous section' },
34 { 'SME_BADREF', 'Malformed reference in "%1"' },
35 { 'SME_BADDEP', 'Malformed dependency specification "%1" in "%i"' },
36 { 'SME_BADVAL', 'Malformed validator specification "%1" in "%i"' },
37 { 'SME_ERRVAL', 'External validator "%1" failed: %2' },
38 { 'SME_VBADPACK', 'Variable "%o" in scheme "%p" references unknown package "%1"' },
39 { 'SME_VBADSECT', 'Variable "%o" in scheme "%p" references unknown section "%1"' },
40 { 'SME_EBADPACK', 'Enum "%v" in scheme "%p" references unknown package "%1"' },
41 { 'SME_EBADSECT', 'Enum "%v" in scheme "%p" references unknown section "%1"' },
42 { 'SME_EBADOPT', 'Enum "%v" in scheme "%p" references unknown option "%1"' },
43 { 'SME_EBADTYPE', 'Enum "%v" in scheme "%p" references non-enum option "%p.%s.%o"' },
44 { 'SME_EBADDEF', 'Enum "%v" in scheme "%p" redeclares the default value of "%p.%s.%o"' },
46 { 'SECT_UNKNOWN', 'Section "%i" (%I) not found in scheme' },
47 { 'SECT_REQUIRED', 'Required section "%p.%S" not found in config' },
48 { 'SECT_UNIQUE', 'Unique section "%p.%S" occurs multiple times in config' },
49 { 'SECT_NAMED', 'The section of type "%p.%S" is stored anonymously in config but must be named' },
50 { 'SECT_NOTFOUND', 'Section "%p.%s" not found in config' },
52 { 'OPT_UNKNOWN', 'Option "%i" (%I) not found in scheme' },
53 { 'OPT_REQUIRED', 'Required option "%i" has no value' },
54 { 'OPT_BADVALUE', 'Value "%v" of option "%i" is not defined in enum %1' },
55 { 'OPT_INVVALUE', 'Value "%v" of given option "%i" does not validate as datatype "%1"' },
56 { 'OPT_NOTLIST', 'Option "%i" is defined as list but stored as plain value' },
57 { 'OPT_DATATYPE', 'Option "%i" has unknown datatype "%1"' },
58 { 'OPT_NOTFOUND', 'Option "%p.%s.%o" not found in config' },
60 { 'DEP_NOTEQUAL', 'Dependency (%1) failed:\nOption "%i" is not eqal "%2"' },
61 { 'DEP_NOVALUE', 'Dependency (%1) failed:\nOption "%i" has no value' },
62 { 'DEP_NOTVALID', 'Dependency (%1) failed:\n%c' },
63 { 'DEP_RECURSIVE', 'Recursive dependency for option "%i" detected' },
64 { 'DEP_BADENUM', 'In dependency check for enum value "%i":\n%c' }
67 -- build error constants and instance constructors
68 for i, v in ipairs(ERRCODES) do
69 luci.uvl.errors[v[1]] = function(...)
73 luci.uvl.errors['ERR_'..v[1]] = i
77 error = luci.util.class()
79 function error.__init__(self, code, pso, args)
82 self.args = ( type(args) == "table" and args or { args } )
84 if luci.util.instanceof( pso, luci.uvl.uvlitem ) then
85 self.stype = pso.sref[2]
86 self.package, self.section, self.option, self.value = unpack(pso.cref)
89 pso = ( type(pso) == "table" and pso or { pso } )
92 local uci = luci.model.uci.cursor()
93 self.stype = uci:get(pso[1], pso[2]) or pso[2]
96 self.package, self.section, self.option, self.value = unpack(pso)
100 function error.child(self, err)
101 if not self.childs then
102 self.childs = { err }
104 table.insert( self.childs, err )
109 function error.string(self,pad)
111 local str = ERRCODES[self.code][2]
112 :gsub("\n", "\n"..pad)
113 :gsub("%%i", self:cid())
114 :gsub("%%I", self:sid())
115 :gsub("%%p", self.package or '<nil>')
116 :gsub("%%s", self.section or '<nil>')
117 :gsub("%%S", self.stype or '<nil>')
118 :gsub("%%o", self.option or '<nil>')
119 :gsub("%%v", self.value or '<nil>')
120 :gsub("%%t", self.object and self.object:type() or '<nil>' )
121 :gsub("%%T", self.object and self.object:title() or '<nil>' )
122 :gsub("%%([1-9])", function(n) error(n) return self.args[tonumber(n)] or '<nil>' end)
126 for _, err in ipairs(self.childs or {}) do
127 s = s .. err:string(pad.." ") .. "\n" .. pad
133 return (str:gsub("%s+$",""))
136 function error.cid(self)
137 return self.object and self.object:cid() or self.package ..
138 ( self.section and '.' .. self.section or '' ) ..
139 ( self.option and '.' .. self.option or '' ) ..
140 ( self.value and '.' .. self.value or '' )
143 function error.sid(self)
144 return self.object and self.object:sid() or self.package ..
145 ( self.stype and '.' .. self.stype or '' ) ..
146 ( self.option and '.' .. self.option or '' ) ..
147 ( self.value and '.' .. self.value or '' )
150 function error.is(self, code)
151 if self.code == code then
153 elseif self.childs then
154 for _, c in ipairs(self.childs) do