Module:Sandbox9

local p = {} local function reciprocal1(value) return 1 / value end function p.test1(frame) local value = frame.args[1] return reciprocal1(value) end local function reciprocal2(value) if value == nil then error('value must exist') end if tonumber(value) == nil then error('value must be a number') end if tonumber(value) == 0 then error('value must not be 0') end return 1 / value end function p.test2(frame) local value = frame.args[1] return reciprocal2(value) end local function reciprocal3(value) assert(value, 'value must exist') assert(tonumber(value), 'value must be a number') assert(tonumber(value) ~= 0, 'value must not be zero') return 1 / value end function p.test3(frame) local value = frame.args[1] return reciprocal3(value) end function p.test4(frame) local value = frame.args[1] -- anonymous unprotected fcn-call w/overriding error-handler if pcall(function result = reciprocal3(value) end) then return result else return 'Error: Value must exist, must be numeric, and not zero.' end end function p.test5(frame) local value = frame.args[1] -- protected-calls always return true/false first... local ok, result = pcall( reciprocal3, value ) if ok then return result else return 'ERROR...msg=' .. result .. '\n' end end local function errHandler(err) return 'Error: Reciprocal failed on value.' end

function p.test6(frame) local msg = '' local value = frame.args[1] -- extended-protection specifies err-handler also... local ok, result = xpcall( reciprocal3, errHandler, value ) if ok then return result else if value then msg = 'ERROR...val=' .. value .. '\t && ' end msg = msg .. 'ERROR...msg=' .. result .. '\n' return msg end end

-- THIS DOES NOT WORK... skip it... function p.test7(frame) local val = frame.args[1] function makeErrHandler (passedValue) return function (e) -- clean up error-message local msgStart = string.find(e, "value") msg = string.sub(e,msgStart) -- add extra private-static msg = msg .. '...value was ' .. passedValue --   return msg return e       end end local EH = makeErrHandler( val ) local ok, result = xpcall( reciprocal3( val ), EH ) if ok then return result else return 'URP...msg=' .. result .. '\n' end end return p