Skip to content

Instantly share code, notes, and snippets.

@Neopallium
Last active December 11, 2015 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neopallium/4536604 to your computer and use it in GitHub Desktop.
Save Neopallium/4536604 to your computer and use it in GitHub Desktop.
Assertion function with formatting support.
-- Release as Public Domain.
local function fmt_error(fmt, ...)
local f_type = type(fmt)
if f_type == 'string' then
if select('#', ...) > 0 then
return string.format(fmt, ...)
else
-- only a single string, no formating needed.
return fmt
end
elseif f_type == 'function' then
-- fmt should be a callable function which returns the message to log
return fmt(...)
end
-- fmt is not a string and not a function, just call tostring() on it.
return tostring(fmt)
end
function assert_fmt(v, fmt, ...)
if v then return v, fmt, ... end
local good, msg = pcall(fmt_error, fmt, ...)
if not good then
msg = "Failed to format error message: " .. msg
end
error(msg, 2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment