libs/uvl: rework error handling to prevent triggering lvm issues
[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)
79         if luci.i18n then
80                 return luci.i18n.translate(key)
81         else
82                 return key
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(ERRCODES[self.code] or self.code])
124                 :gsub("\n", "\n"..pad)
125                 :gsub("%%i", self:cid())
126                 :gsub("%%I", self:sid())
127                 :gsub("%%p", self.package or '(nil)')
128                 :gsub("%%s", self.section or '(nil)')
129                 :gsub("%%S", self.stype   or '(nil)')
130                 :gsub("%%o", self.option  or '(nil)')
131                 :gsub("%%v", self.value   or '(nil)')
132                 :gsub("%%t", self.object and self.object:type()  or '(nil)' )
133                 :gsub("%%T", self.object and self.object:title() or '(nil)' )
134                 :gsub("%%([1-9])", function(n) return self.args[tonumber(n)] or '(nil)' end)
135                 :gsub("%%c",
136                         function()
137                                 local s = ""
138                                 for _, err in ipairs(self.childs or {}) do
139                                         s = s .. err:string(pad.."  ") .. "\n" .. pad
140                                 end
141                                 return s
142                         end
143                 )
144
145         return (str:gsub("%s+$",""))
146 end
147
148 function error.cid(self)
149         return self.object and self.object:cid() or self.package ..
150                 ( self.section and '.' .. self.section or '' ) ..
151                 ( self.option  and '.' .. self.option  or '' ) ..
152                 ( self.value   and '.' .. self.value   or '' )
153 end
154
155 function error.sid(self)
156         return self.object and self.object:sid() or self.package ..
157                 ( self.stype   and '.' .. self.stype   or '' ) ..
158                 ( self.option  and '.' .. self.option  or '' ) ..
159                 ( self.value   and '.' .. self.value   or '' )
160 end
161
162 function error.is(self, code)
163         if self.code == code then
164                 return true
165         elseif self.childs then
166                 for _, c in ipairs(self.childs) do
167                         if c:is(code) then
168                                 return true
169                         end
170                 end
171         end
172         return false
173 end
174
175 function error.is_all(self, ...)
176         local codes = { ... }
177
178         if util.contains(codes, self.code) then
179                 return true
180         else
181                 local equal = false
182                 for _, c in ipairs(self.childs) do
183                         if c.childs then
184                                 equal = c:is_all(...)
185                         else
186                                 equal = util.contains(codes, c.code)
187                         end
188                 end
189                 return equal
190         end
191 end