8e0d78ad19526c1a2ddbeffb035efb0778e2a3d1
[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 module( "luci.uvl.errors", package.seeall )
18
19 require("luci.util")
20
21
22 ERRCODES = {
23         { 'UCILOAD',            'Unable to load config "%p": %1' },
24
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' },
31
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"' },
47
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' },
53
54         { 'OPT_UNKNOWN',        'Option "%i" (%I) not found in scheme' },
55         { 'OPT_REQUIRED',       'Required option "%i" has no value' },
56         { 'OPT_BADVALUE',       'Value "%1" of option "%i" is not defined in enum %2' },
57         { 'OPT_INVVALUE',       'Value "%1" of option "%i" does not validate as datatype "%2"' },
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' },
61
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' }
67 }
68
69 -- build error constants and instance constructors
70 for i, v in ipairs(ERRCODES) do
71         luci.uvl.errors[v[1]] = function(...)
72                 return error(i, ...)
73         end
74
75         luci.uvl.errors['ERR_'..v[1]] = i
76 end
77
78
79 function i18n(key, def)
80         if luci.i18n then
81                 return luci.i18n.translate(key,def)
82         else
83                 return def
84         end
85 end
86
87
88 error = luci.util.class()
89
90 function error.__init__(self, code, pso, args)
91
92         self.code = code
93         self.args = ( type(args) == "table" and args or { args } )
94
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)
98                 self.object = pso
99                 self.value  = self.value or ( pso.value and pso:value() )
100         else
101                 pso = ( type(pso) == "table" and pso or { pso } )
102
103                 if pso[2] then
104                         local uci = luci.model.uci.cursor()
105                         self.stype = uci:get(pso[1], pso[2]) or pso[2]
106                 end
107
108                 self.package, self.section, self.option, self.value = unpack(pso)
109         end
110 end
111
112 function error.child(self, err)
113         if not self.childs then
114                 self.childs = { err }
115         else
116                 table.insert( self.childs, err )
117         end
118         return self
119 end
120
121 function error.string(self,pad)
122         pad = pad or "  "
123
124         local str = i18n(
125                 'uvl_err_%s' % string.lower(ERRCODES[self.code][1]),
126                 ERRCODES[self.code][2]
127         )
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)
139                 :gsub("%%c",
140                         function()
141                                 local s = ""
142                                 for _, err in ipairs(self.childs or {}) do
143                                         s = s .. err:string(pad.."  ") .. "\n" .. pad
144                                 end
145                                 return s
146                         end
147                 )
148
149         return (str:gsub("%s+$",""))
150 end
151
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 '' )
157 end
158
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 '' )
164 end
165
166 function error.is(self, code)
167         if self.code == code then
168                 return true
169         elseif self.childs then
170                 for _, c in ipairs(self.childs) do
171                         if c:is(code) then
172                                 return true
173                         end
174                 end
175         end
176         return false
177 end