luci-0.9: merge r5861-r5863
[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 function i18n(key, def)
79 if luci.i18n then
80 return luci.i18n.translate(key,def)
81 else
82 return def
83 end
84 end
85
86
87 error = util.class()
88
89 function error.__init__(self, code, pso, args)
90
91 self.code = code
92 self.args = ( type(args) == "table" and args or { args } )
93
94 if util.instanceof( pso, uvl.uvlitem ) then
95 self.stype = pso.sref[2]
96 self.package, self.section, self.option, self.value = unpack(pso.cref)
97 self.object = pso
98 self.value = self.value or ( pso.value and pso:value() )
99 else
100 pso = ( type(pso) == "table" and pso or { pso } )
101
102 if pso[2] then
103 local uci = uci.cursor()
104 self.stype = uci:get(pso[1], pso[2]) or pso[2]
105 end
106
107 self.package, self.section, self.option, self.value = unpack(pso)
108 end
109 end
110
111 function error.child(self, err)
112 if not self.childs then
113 self.childs = { err }
114 else
115 self.childs[#self.childs+1] = err
116 end
117 return self
118 end
119
120 function error.string(self,pad)
121 pad = pad or " "
122
123 local str = i18n(
124 'uvl_err_%s' % string.lower(self.code),
125 ERRCODES[self.code]
126 )
127 :gsub("\n", "\n"..pad)
128 :gsub("%%i", self:cid())
129 :gsub("%%I", self:sid())
130 :gsub("%%p", self.package or '(nil)')
131 :gsub("%%s", self.section or '(nil)')
132 :gsub("%%S", self.stype or '(nil)')
133 :gsub("%%o", self.option or '(nil)')
134 :gsub("%%v", self.value or '(nil)')
135 :gsub("%%t", self.object and self.object:type() or '(nil)' )
136 :gsub("%%T", self.object and self.object:title() or '(nil)' )
137 :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '(nil)' end)
138 :gsub("%%c",
139 function()
140 local s = ""
141 for _, err in ipairs(self.childs or {}) do
142 s = s .. err:string(pad.." ") .. "\n" .. pad
143 end
144 return s
145 end
146 )
147
148 return (str:gsub("%s+$",""))
149 end
150
151 function error.cid(self)
152 return self.object and self.object:cid() or self.package ..
153 ( self.section and '.' .. self.section or '' ) ..
154 ( self.option and '.' .. self.option or '' ) ..
155 ( self.value and '.' .. self.value or '' )
156 end
157
158 function error.sid(self)
159 return self.object and self.object:sid() or self.package ..
160 ( self.stype and '.' .. self.stype or '' ) ..
161 ( self.option and '.' .. self.option or '' ) ..
162 ( self.value and '.' .. self.value or '' )
163 end
164
165 function error.is(self, code)
166 if self.code == code then
167 return true
168 elseif self.childs then
169 for _, c in ipairs(self.childs) do
170 if c:is(code) then
171 return true
172 end
173 end
174 end
175 return false
176 end
177
178 function error.is_all(self, ...)
179 local codes = { ... }
180
181 if util.contains(codes, self.code) then
182 return true
183 else
184 local equal = false
185 for _, c in ipairs(self.childs) do
186 if c.childs then
187 equal = c:is_all(...)
188 else
189 equal = util.contains(codes, c.code)
190 end
191 end
192 return equal
193 end
194 end