]> git.e2factory.org Git - e2factory.git/commitdiff
Change the err module to a more encapsulated type
authorTobias Ulmer <tu@emlix.com>
Wed, 8 Aug 2012 12:19:19 +0000 (14:19 +0200)
committerTobias Ulmer <tu@emlix.com>
Mon, 13 Aug 2012 10:22:07 +0000 (12:22 +0200)
Also rename new_error() to err.new() and remove it from the global
environment.

Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/err.lua

index b93ed1f126c2f7c251aa3fc0ae7da370051f1d48..6586969c3195499e540acff5ef44e0935545aebf 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ]]
 
-
-module("err", package.seeall)
+local err =  {}
 
 --- 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 append(e, format, ...)
+function err.append(e, format, ...)
        e.count = e.count + 1
        table.insert(e.msg, string.format(format, ...))
        return e
@@ -42,24 +41,29 @@ end
 -- @param e table: the error object
 -- @param re table: the error object to insert
 -- @return table: the error object
-function cat(e, re)
+function err.cat(e, re)
        -- auto-convert strings to error objects before inserting
        if type(re) == "string" then
-               re = new_error("%s", re)
+               re = err.new("%s", re)
        end
        table.insert(e.msg, re)
        e.count = e.count + 1
        return e
 end
 
-function print(e, depth)
+--- print error messages at log level 1
+-- @param e table: the error object
+-- @param depth number: used internally to count and display nr. of sub errors
+function err.print(e, depth)
        if not depth then
                depth = 1
        else
                depth = depth + 1
        end
+
        local prefix = string.format("Error [%d]: ", depth)
-       for _,m in ipairs(e.msg) do
+
+       for k,m in ipairs(e.msg) do
                if type(m) == "string" then
                        e2lib.log(1, string.format("%s%s", prefix, m))
                        prefix = string.format("      [%d]: ", depth)
@@ -74,14 +78,14 @@ end
 -- @param e the error object
 -- @param n number: new error counter setting
 -- @return nil
-function setcount(e, n)
+function err.setcount(e, n)
        e.count = n
 end
 
 --- get the error counter
 -- @param e the error object
 -- @return number: the error counter
-function getcount(e, n)
+function err.getcount(e, n)
        return e.count
 end
 
@@ -89,7 +93,7 @@ end
 -- @param format string: format string
 -- @param ... list of arguments required for the format string
 -- @return table: the error object
-function new_error(format, ...)
+function err.new(format, ...)
        local e = {}
        e.count = 0
        e.msg = {}
@@ -101,12 +105,12 @@ function new_error(format, ...)
        return e
 end
 
-function toerror(x)
+function err.toerror(x)
        if type(x) == "table" then
                return x
        else
-               return new_error(x)
+               return err.new(x)
        end
 end
 
-_G.new_error = new_error
+return err