* luci/libs: uvl: Major rewrite of internal element handling, reworked error model...
[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 require("luci.util")
19
20 ERRCODES = {
21         { 'UCILOAD',            'Unable to load config "%p"' },
22
23         { 'SCHEME',                     'Error in scheme "%p":\n%c' },
24         { 'CONFIG',             'Error in config "%p":\n%c' },
25         { 'SECTION',            'Error in section "%p.%s":\n%c' },
26         { 'OPTION',                     'Error in option "%p.%s.%o":\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' },
29
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"' },
45
46         { 'SECT_UNKNOWN',       'Section "%p.%s" 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' },
51
52         { 'OPT_UNKNOWN',        'Option "%1" not found in scheme' },
53         { 'OPT_REQUIRED',       'Required option "%i" has no value' },
54         { 'OPT_BADVALUE',       'Value "%1" of option "%i" is not defined in %t { %2 }' },
55         { 'OPT_INVVALUE',       'Value "%1" of given option "%i" does not validate as datatype "%2"' },
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' },
59
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' }
65 }
66
67 -- build error constants
68 for i, v in ipairs(ERRCODES) do
69         luci.uvl.errors[v[1]] = function(...)
70                 return error(i, ...)
71         end
72 end
73
74
75 error = luci.util.class()
76
77 function error.__init__(self, code, pso, args)
78
79         self.code = code
80         self.args = ( type(args) == "table" and args or { args } )
81
82         if luci.util.instanceof( pso, luci.uvl.uvlitem ) then
83                 self.stype = pso.sref[2]
84                 self.package, self.section, self.option, self.value = unpack(pso.cref)
85                 self.object = pso
86         else
87                 pso = ( type(pso) == "table" and pso or { pso } )
88
89                 if pso[2] then
90                         local uci = luci.model.uci.cursor()
91                         self.stype = uci:get(pso[1], pso[2]) or pso[2]
92                 end
93
94                 self.package, self.section, self.option, self.value = unpack(pso)
95         end
96 end
97
98 function error.child(self, err)
99         if not self.childs then
100                 self.childs = { err }
101         else
102                 table.insert( self.childs, err )
103         end
104         return self
105 end
106
107 function error.string(self,pad)
108         pad = pad or "  "
109         local str = ERRCODES[self.code][2]
110                 :gsub("\n", "\n"..pad)
111                 :gsub("%%i", self:cid())
112                 :gsub("%%I", self:sid())
113                 :gsub("%%p", self.package or '<nil>')
114                 :gsub("%%s", self.section or '<nil>')
115                 :gsub("%%S", self.stype   or '<nil>')
116                 :gsub("%%o", self.option  or '<nil>')
117                 :gsub("%%v", self.value   or '<nil>')
118                 :gsub("%%t", self.object and self.object:type()  or '<nil>' )
119                 :gsub("%%T", self.object and self.object:title() or '<nil>' )
120                 :gsub("%%([1-9])", function(n) error(n) return self.args[tonumber(n)] or '<nil>' end)
121                 :gsub("%%c",
122                         function()
123                                 local s = ""
124                                 for _, err in ipairs(self.childs or {}) do
125                                         s = s .. err:string(pad.."  ") .. "\n" .. pad
126                                 end
127                                 return s
128                         end
129                 )
130
131         return (str:gsub("%s+$",""))
132 end
133
134 function error.cid(self)
135         return self.object and self.object:cid() or self.package ..
136                 ( self.section and '.' .. self.section or '' ) ..
137                 ( self.option  and '.' .. self.option  or '' ) ..
138                 ( self.value   and '.' .. self.value   or '' )
139 end
140
141 function error.sid(self)
142         return self.object and self.object:sid() or self.package ..
143                 ( self.stype   and '.' .. self.stype   or '' ) ..
144                 ( self.option  and '.' .. self.option  or '' ) ..
145                 ( self.value   and '.' .. self.value   or '' )
146 end