From: Tobias Ulmer Date: Tue, 9 Dec 2014 20:25:03 +0000 (+0100) Subject: err: add asserts X-Git-Tag: e2factory-2.3.15rc1~184 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=caaa4cf5301fc24771501a24a145604a7371ca0b;p=e2factory.git err: add asserts Signed-off-by: Tobias Ulmer --- diff --git a/generic/err.lua b/generic/err.lua index 6af9785..a64143c 100644 --- a/generic/err.lua +++ b/generic/err.lua @@ -32,11 +32,20 @@ local err = {} local e2lib = require("e2lib") local strict = require("strict") +local function assert_err(e) + assert(type(e) == "table") + assert(type(e.count) == "number") + assert(type(e.msg) == "table") + return true +end + --- append a string to an error object -- @param format string: format string -- @param ... list of strings required for the format string -- @return table: the error object function err.append(e, format, ...) + assert_err(e) + assert(type(format) == "string") e.count = e.count + 1 table.insert(e.msg, string.format(format, ...)) return e @@ -47,6 +56,8 @@ end -- @param re table: the error object to insert -- @return table: the error object function err.cat(e, re) + assert_err(e) + assert(type(re) == "string" or assert_err(re)) -- auto-convert strings to error objects before inserting if type(re) == "string" then re = err.new("%s", re) @@ -60,6 +71,8 @@ end -- @param e table: the error object -- @param depth number: used internally to count and display nr. of sub errors function err.print(e, depth) + assert_err(e) + assert(type(depth) == "number" or depth == nil) if not depth then depth = 1 else @@ -84,14 +97,17 @@ end -- @param n number: new error counter setting -- @return nil function err.setcount(e, n) + assert_err(e) + assert(type(n) == "number") e.count = n end --- get the error counter -- @param e the error object --- @param n unused -- @return number: the error counter function err.getcount(e, n) + assert_err(e) + assert(n == nil) -- unused, spot uses return e.count end @@ -100,6 +116,7 @@ end -- @param ... list of arguments required for the format string -- @return table: the error object function err.new(format, ...) + assert(type(format) == "string" or format == nil) local e = {} e.count = 0 e.msg = {} @@ -111,14 +128,6 @@ function err.new(format, ...) return e end -function err.toerror(x) - if type(x) == "table" then - return x - else - return err.new(x) - end -end - return strict.lock(err) -- vim:sw=4:sts=4:et: