From aec67b4e608465017ad67665f8cd1f37ae35d6cc Mon Sep 17 00:00:00 2001 From: aalatchm Date: Wed, 30 Mar 2011 13:41:45 +0000 Subject: Modified parse() function to avoid throwing exceptions. Lua 'error()' function was previously used, but this throws exceptions. If the user does not catch the exceptions, then his code will exit. This commit replaces the 'error()' function with 'return nil, errtxt', which follows the pattern of many Lua functions, such as 'io.open()'. The user can then choose to handle the situation in a manner appropriate for his code. To maintain the previous behavior, the user may call 'assert(posix.parse(...))'. --- posixtz.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/posixtz.lua b/posixtz.lua index ae84275..eb1e56e 100644 --- a/posixtz.lua +++ b/posixtz.lua @@ -13,7 +13,6 @@ -- Import Section -- declare everything this module needs from outside -local error = error local tonumber = tonumber local pairs = pairs local ipairs = ipairs @@ -33,7 +32,7 @@ function parse ( str ) local temp = {} if not str then - error('Nothing to parse') + return nil, 'Nothing to parse' end @@ -50,7 +49,7 @@ function parse ( str ) -- tz.std must be defined by now if not tz.std then - error('Not a valid POSIX TZ. "std" invalid') + return nil, 'Not a valid POSIX TZ. "std" invalid' end @@ -73,7 +72,7 @@ function parse ( str ) end else - error('Not a valid POSIX TZ. "offset" invalid') + return nil, 'Not a valid POSIX TZ. "offset" invalid' end @@ -95,14 +94,14 @@ function parse ( str ) tz.dst.name, temp.afterdst = string.match(temp.afteroffset, '^(%a+)(.*)') end if not tz.dst.name then - error('Not a valid POSIX TZ. "dst" present but not valid') + return nil, 'Not a valid POSIX TZ. "dst" present but not valid' end -- tz.dst.name is defined, so process "rule" -- Parse out specifications from "rule" temp.dststart, temp.dststop = string.match(temp.afterdst, '^,([^,]+),([^,]+)') if not temp.dststart then - error('Not a valid POSIX TZ. "dst" present but "rule" is not') + return nil, 'Not a valid POSIX TZ. "dst" present but "rule" is not' end @@ -178,7 +177,7 @@ function parse ( str ) -- Validation: t.month must be defined by now. if not t.month then - error('Not a valid POSIX TZ. "rule" for DST "'..pos..'" is invalid') + return nil, 'Not a valid POSIX TZ. "rule" for DST "'..pos..'" is invalid' end end -- cgit v1.2.3