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