* luci/libs/uvl:
[project/luci.git] / libs / uvl / luasrc / uvl / errors.lua
1 --[[
2
3 UCI Validation Layer - Error handling
4 (c) 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 local uci = require "luci.model.uci"
18 local uvl = require "luci.uvl"
19 local util = require "luci.util"
20 local string = require "string"
21
22 local ipairs, error, type = ipairs, error, type
23 local tonumber, unpack = tonumber, unpack
24
25
26 local luci = luci
27
28 module "luci.uvl.errors"
29
30 ERRCODES = {
31 { 'UCILOAD', 'Unable to load config "%p": %1' },
32
33 { 'SCHEME', 'Error in scheme "%p":\n%c' },
34 { 'CONFIG', 'Error in config "%p":\n%c' },
35 { 'SECTION', 'Error in section "%i" (%I):\n%c' },
36 { 'OPTION', 'Error in option "%i" (%I):\n%c' },
37 { 'REFERENCE', 'Option "%i" has invalid reference specification %1:\n%c' },
38 { 'DEPENDENCY', 'In dependency check for %t "%i":\n%c' },
39
40 { 'SME_FIND', 'Can not find scheme "%p" in "%1"' },
41 { 'SME_READ', 'Can not access file "%1"' },
42 { 'SME_REQFLD', 'Missing required scheme field "%1" in "%i"' },
43 { 'SME_INVREF', 'Illegal reference "%1" to an anonymous section' },
44 { 'SME_BADREF', 'Malformed reference in "%1"' },
45 { 'SME_BADDEP', 'Malformed dependency specification "%1" in "%i"' },
46 { 'SME_BADVAL', 'Malformed validator specification "%1" in "%i"' },
47 { 'SME_ERRVAL', 'External validator "%1" failed: %2' },
48 { 'SME_VBADPACK', 'Variable "%o" in scheme "%p" references unknown package "%1"' },
49 { 'SME_VBADSECT', 'Variable "%o" in scheme "%p" references unknown section "%1"' },
50 { 'SME_EBADPACK', 'Enum "%v" in scheme "%p" references unknown package "%1"' },
51 { 'SME_EBADSECT', 'Enum "%v" in scheme "%p" references unknown section "%1"' },
52 { 'SME_EBADOPT', 'Enum "%v" in scheme "%p" references unknown option "%1"' },
53 { 'SME_EBADTYPE', 'Enum "%v" in scheme "%p" references non-enum option "%I"' },
54 { 'SME_EBADDEF', 'Enum "%v" in scheme "%p" redeclares the default value of "%I"' },
55
56 { 'SECT_UNKNOWN', 'Section "%i" (%I) not found in scheme' },
57 { 'SECT_REQUIRED', 'Required section "%p.%S" not found in config' },
58 { 'SECT_UNIQUE', 'Unique section "%p.%S" occurs multiple times in config' },
59 { 'SECT_NAMED', 'The section of type "%p.%S" is stored anonymously in config but must be named' },
60 { 'SECT_NOTFOUND', 'Section "%p.%s" not found in config' },
61
62 { 'OPT_UNKNOWN', 'Option "%i" (%I) not found in scheme' },
63 { 'OPT_REQUIRED', 'Required option "%i" has no value' },
64 { 'OPT_BADVALUE', 'Value "%1" of option "%i" is not defined in enum %2' },
65 { 'OPT_INVVALUE', 'Value "%1" of option "%i" does not validate as datatype "%2"' },
66 { 'OPT_NOTLIST', 'Option "%i" is defined as list but stored as plain value' },
67 { 'OPT_DATATYPE', 'Option "%i" has unknown datatype "%1"' },
68 { 'OPT_NOTFOUND', 'Option "%p.%s.%o" not found in config' },
69 { 'OPT_RANGE', 'Option "%p.%s.%o" is not within the specified range' },
70
71 { 'DEP_NOTEQUAL', 'Dependency (%1) failed:\nOption "%i" is not eqal "%2"' },
72 { 'DEP_NOVALUE', 'Dependency (%1) failed:\nOption "%i" has no value' },
73 { 'DEP_NOTVALID', 'Dependency (%1) failed:\n%c' },
74 { 'DEP_RECURSIVE', 'Recursive dependency for option "%i" detected' },
75 { 'DEP_BADENUM', 'In dependency check for enum value "%i":\n%c' }
76 }
77
78 -- build error constants and instance constructors
79 for i, v in ipairs(ERRCODES) do
80 _M[v[1]] = function(...)
81 return error(i, ...)
82 end
83
84 _M['ERR_'..v[1]] = i
85 end
86
87
88 function i18n(key, def)
89 if luci.i18n then
90 return luci.i18n.translate(key,def)
91 else
92 return def
93 end
94 end
95
96
97 error = util.class()
98
99 function error.__init__(self, code, pso, args)
100
101 self.code = code
102 self.args = ( type(args) == "table" and args or { args } )
103
104 if util.instanceof( pso, uvl.uvlitem ) then
105 self.stype = pso.sref[2]
106 self.package, self.section, self.option, self.value = unpack(pso.cref)
107 self.object = pso
108 self.value = self.value or ( pso.value and pso:value() )
109 else
110 pso = ( type(pso) == "table" and pso or { pso } )
111
112 if pso[2] then
113 local uci = uci.cursor()
114 self.stype = uci:get(pso[1], pso[2]) or pso[2]
115 end
116
117 self.package, self.section, self.option, self.value = unpack(pso)
118 end
119 end
120
121 function error.child(self, err)
122 if not self.childs then
123 self.childs = { err }
124 else
125 self.childs[#self.childs+1] = err
126 end
127 return self
128 end
129
130 function error.string(self,pad)
131 pad = pad or " "
132
133 local str = i18n(
134 'uvl_err_%s' % string.lower(ERRCODES[self.code][1]),
135 ERRCODES[self.code][2]
136 )
137 :gsub("\n", "\n"..pad)
138 :gsub("%%i", self:cid())
139 :gsub("%%I", self:sid())
140 :gsub("%%p", self.package or '(nil)')
141 :gsub("%%s", self.section or '(nil)')
142 :gsub("%%S", self.stype or '(nil)')
143 :gsub("%%o", self.option or '(nil)')
144 :gsub("%%v", self.value or '(nil)')
145 :gsub("%%t", self.object and self.object:type() or '(nil)' )
146 :gsub("%%T", self.object and self.object:title() or '(nil)' )
147 :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '(nil)' end)
148 :gsub("%%c",
149 function()
150 local s = ""
151 for _, err in ipairs(self.childs or {}) do
152 s = s .. err:string(pad.." ") .. "\n" .. pad
153 end
154 return s
155 end
156 )
157
158 return (str:gsub("%s+$",""))
159 end
160
161 function error.cid(self)
162 return self.object and self.object:cid() or self.package ..
163 ( self.section and '.' .. self.section or '' ) ..
164 ( self.option and '.' .. self.option or '' ) ..
165 ( self.value and '.' .. self.value or '' )
166 end
167
168 function error.sid(self)
169 return self.object and self.object:sid() or self.package ..
170 ( self.stype and '.' .. self.stype or '' ) ..
171 ( self.option and '.' .. self.option or '' ) ..
172 ( self.value and '.' .. self.value or '' )
173 end
174
175 function error.is(self, code)
176 if self.code == code then
177 return true
178 elseif self.childs then
179 for _, c in ipairs(self.childs) do
180 if c:is(code) then
181 return true
182 end
183 end
184 end
185 return false
186 end
187
188 function error.is_all(self, ...)
189 local codes = { ... }
190
191 if util.contains(codes, self.code) then
192 return true
193 else
194 local equal = false
195 for _, c in ipairs(self.childs) do
196 if c.childs then
197 equal = c:is_all(...)
198 else
199 equal = util.contains(codes, c.code)
200 end
201 end
202 return equal
203 end
204 end