f3ae4b91468e97c04cf5d0308187ac42fd4069a7
[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" (syntax error or file not found)' },
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 and instance constructors
68 for i, v in ipairs(ERRCODES) do
69         luci.uvl.errors[v[1]] = function(...)
70                 return error(i, ...)
71         end
72
73         luci.uvl.errors['ERR_'..v[1]] = i
74 end
75
76
77 error = luci.util.class()
78
79 function error.__init__(self, code, pso, args)
80
81         self.code = code
82         self.args = ( type(args) == "table" and args or { args } )
83
84         if luci.util.instanceof( pso, luci.uvl.uvlitem ) then
85                 self.stype = pso.sref[2]
86                 self.package, self.section, self.option, self.value = unpack(pso.cref)
87                 self.object = pso
88         else
89                 pso = ( type(pso) == "table" and pso or { pso } )
90
91                 if pso[2] then
92                         local uci = luci.model.uci.cursor()
93                         self.stype = uci:get(pso[1], pso[2]) or pso[2]
94                 end
95
96                 self.package, self.section, self.option, self.value = unpack(pso)
97         end
98 end
99
100 function error.child(self, err)
101         if not self.childs then
102                 self.childs = { err }
103         else
104                 table.insert( self.childs, err )
105         end
106         return self
107 end
108
109 function error.string(self,pad)
110         pad = pad or "  "
111         local str = ERRCODES[self.code][2]
112                 :gsub("\n", "\n"..pad)
113                 :gsub("%%i", self:cid())
114                 :gsub("%%I", self:sid())
115                 :gsub("%%p", self.package or '<nil>')
116                 :gsub("%%s", self.section or '<nil>')
117                 :gsub("%%S", self.stype   or '<nil>')
118                 :gsub("%%o", self.option  or '<nil>')
119                 :gsub("%%v", self.value   or '<nil>')
120                 :gsub("%%t", self.object and self.object:type()  or '<nil>' )
121                 :gsub("%%T", self.object and self.object:title() or '<nil>' )
122                 :gsub("%%([1-9])", function(n) error(n) return self.args[tonumber(n)] or '<nil>' end)
123                 :gsub("%%c",
124                         function()
125                                 local s = ""
126                                 for _, err in ipairs(self.childs or {}) do
127                                         s = s .. err:string(pad.."  ") .. "\n" .. pad
128                                 end
129                                 return s
130                         end
131                 )
132
133         return (str:gsub("%s+$",""))
134 end
135
136 function error.cid(self)
137         return self.object and self.object:cid() or self.package ..
138                 ( self.section and '.' .. self.section or '' ) ..
139                 ( self.option  and '.' .. self.option  or '' ) ..
140                 ( self.value   and '.' .. self.value   or '' )
141 end
142
143 function error.sid(self)
144         return self.object and self.object:sid() or self.package ..
145                 ( self.stype   and '.' .. self.stype   or '' ) ..
146                 ( self.option  and '.' .. self.option  or '' ) ..
147                 ( self.value   and '.' .. self.value   or '' )
148 end